Quick Question

Tim McDaniel mcdaniel at adi.com
Thu Aug 16 01:39:15 AEST 1990


martin at mwtech.UUCP (Martin Weitzel) writes:

   You can ALLWAYS start with the name of the object (here: a), end
   with the type (here: int) and simply build your type description
   by concatenating what you encounter on your way "inside to out".
   So the first is the correct parsing.

   Because of this some people even prefer

           int const x; /* would read: x is a constant int */
   over
           const int x; /* would read: x is an int constant */

   Both have the same meaning in ANSI C - the first seems more "logical",
   but the second is (still) in more widespread use.

I thought so too, until I read K&R2 more closely.  In the example
given above, "const" 'binds' to "int" rather than "x".  So it differs
from the treatment of "*" in declarations (the only prefix operator in
declarations):

   int *     ip, i;	/* ip is pointer to int, i is int */
   int const ci, ci2;	/* ci is const int, ci2 IS ALSO CONST INT */

Of less importance, "const" and "volatile" are special; in the middle
of a declaration, they can only appear immediately after a "*" token.

   int *p, (*p2), *(p3);   /* all are pointers to int */
   int * const cpi;	   /* cpi is const pointer to int */
   int (* const cpi2);	   /* cpi2 is const pointer to int */
   int *(const cpi3);	   /* illegal: syntax error */

So you can't just use the inside-out construction rules blindly here:
you must remember that "const" is part of the type identifier, so
I consider "int const" to be misleading.

I think it would have been more consistent to treat "const" and
"volatile" in declarations just like "*", as a prefix unary operator.
It's now far too late.

--
--
Tim McDaniel
Internet: mcdaniel at adi.com             UUCP: {uunet,sharkey}!amara!mcdaniel



More information about the Comp.std.c mailing list