On 09/24/13 10:46, Laine Stump wrote:
On 09/23/2013 08:03 PM, Laszlo Ersek wrote:
> ... and adapt functions that would cast away the new const qualifier.
>
> Given
>
> typedef virSocketAddr *virSocketAddrPtr;
>
> Compare the parse trees of the following two declarations:
>
> (a) const virSocketAddrPtr addr;
> (b) const virSocketAddr *addr;
Umm.. Eric? A little help? :-)
The grammar rules that I used for the AST derivation can be looked up
eg. in the final C11 draft,
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
Section 6.7 "Declarations".
But, the short version is really just that type qualifiers (like const &
volatile) don't enter the typedef name; they qualify the variable being
declared.
const virSocketAddrPtr addr;
virSocketAddrPtr const addr;
these are equivalent, they mean the same thing, a constant pointer to a
variable object. Expanding the typedef, that's written as
virSocketAddr *const addr;
(It is closer in appearance to the second form above.)
If you want to qualify the target of the pointer, you must say one of
the following:
(i) const virSocketAddr *addr;
(ii) virSocketAddr const *addr;
(iii) typedef const virSocketAddr *constVirSocketAddrPtr;
constVirSocketAddrPtr addr;
(iv) typedef virSocketAddr const *constVirSocketAddrPtr;
constVirSocketAddrPtr addr;
In general I disapprove of typedefs: they seem to be friendly by saving
you the repeated typing of "struct" and "*". Until they trick you :)
Laszlo