[comp.std.c] Quick Question

hjelm@g.gp.cs.cmu.edu (Mark Hjelm) (08/07/90)

What is the correct parse of:

    int *const *volatile a;

a is a volatile pointer to a const pointer to an int or
a is a const pointer to a volatile pointer to an int?


Mark
hjelm@cs.cmu.edu

bright@sctc.com (Dave Bright) (08/08/90)

hjelm@g.gp.cs.cmu.edu (Mark Hjelm) writes:
>What is the correct parse of:
>    int *const *volatile a;
>a is a volatile pointer to a const pointer to an int or
>a is a const pointer to a volatile pointer to an int?

According to cdecl, choice #1:
$ cdecl
Type `help' or `?' for help
cdecl> explain int *const *volatile a;
declare a as volatile pointer to const pointer to int
$

>hjelm@cs.cmu.edu
-- 
David A. Bright
Secure Computing Technology Corporation		bright@SCTC.COM		(work)
1210 West County Road E, Suite 100		dab@Bright.MN.ORG	(home)
Arden Hills, MN  55112

martin@mwtech.UUCP (Martin Weitzel) (08/09/90)

In article <10136@pt.cs.cmu.edu> hjelm@g.gp.cs.cmu.edu (Mark Hjelm) writes:
>
>
>What is the correct parse of:
>
>    int *const *volatile a;
>
>a is a volatile pointer to a const pointer to an int or
>a is a const pointer to a volatile pointer to an int?

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.
-- 
Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83

gwyn@smoke.BRL.MIL (Doug Gwyn) (08/09/90)

In article <10136@pt.cs.cmu.edu> hjelm@g.gp.cs.cmu.edu (Mark Hjelm) writes:
-What is the correct parse of:
-    int *const *volatile a;
-a is a volatile pointer to a const pointer to an int or
-a is a const pointer to a volatile pointer to an int?

The former.  C declaration style is "contextual", so that whatever is
closest to the identifier happens first.

mcdaniel@adi.com (Tim McDaniel) (08/15/90)

martin@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@adi.com             UUCP: {uunet,sharkey}!amara!mcdaniel