[comp.std.c] Volatile binding for const?

pardo@uw-june.stars.flab.Fujitsu.JUNET (David Keppel) (03/29/89)

I'm confused about the proper binding for the `const' and `volatile'
keywords.  Both of my `pANS conformant' (:-) compilers tell me
something about storage qualifiers that I find counter-intuitive.

	char const *s, *t;	=is=>  {char const *} s, t
	char *const s, *t;	=is=>  char {const *s}, *t

The second one makes sense to me.  The first one doesn't.  Is this
behavior correct?  Is there a rationalle that would help my
intuition get a little closer to reality?

	;-D on  ( temper_t volatile * const hothead; )  Pardo
--
		    pardo@cs.washington.edu
    {rutgers,cornell,ucsd,ubc-cs,tektronix}!uw-beaver!june!pardo

gwyn@smoke.BRL.MIL (Doug Gwyn ) (03/30/89)

In article <PARDO.89Mar28105228@uw-june.stars.flab.Fujitsu.JUNET> pardo@uw-june.stars.flab.Fujitsu.JUNET (David Keppel) writes:
>I'm confused about the proper binding for the `const' and `volatile'
>keywords.  Both of my `pANS conformant' (:-) compilers tell me
>something about storage qualifiers that I find counter-intuitive.
>	char const *s, *t;	=is=>  {char const *} s, t
>	char *const s, *t;	=is=>  char {const *s}, *t
>The second one makes sense to me.  The first one doesn't.  Is this
>behavior correct?  Is there a rationalle that would help my
>intuition get a little closer to reality?

Perhaps it will help to observe that `s' has a different type in the
two cases.  In the first case it is a pointer to a const char, while
in the second case it is a const pointer to a char.

Thus, "const" in the first case applies to "char" in effect, and is
thus part of the types for both `s' and `t'.  In the second case,
"const" applies to the "*" associated with `s' but not to the "*"
associated with `t'.

henry@utzoo.uucp (Henry Spencer) (03/30/89)

In article <PARDO.89Mar28105228@uw-june.stars.flab.Fujitsu.JUNET> pardo@uw-june.stars.flab.Fujitsu.JUNET (David Keppel) writes:
>	char const *s, *t;	=is=>  {char const *} s, t
>	char *const s, *t;	=is=>  char {const *s}, *t
>
>The second one makes sense to me.  The first one doesn't.  Is this
>behavior correct?

Yes.  "char const" in the first declaration is exactly the same as
"const char" -- the "const" is part of the 'basic' type, not the
declarators for the individual variables.  In the second one, the
"const" is part of the declarator.  The first one declares "s" to
be pointer to const char.  The second one declares "s" to be const
pointer to char.  (Both declare "t" to be pointer to const char.)
There are two places where "const" may appear -- as part of the
'basic' type, and as part of a declarator (AFTER the "*"), and the
meanings are different.

Few will dispute that the syntax is revolting, even for C.
-- 
Welcome to Mars!  Your         |     Henry Spencer at U of Toronto Zoology
passport and visa, comrade?    | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

henry@utzoo.uucp (Henry Spencer) (04/02/89)

In article <1989Mar29.210035.4085@utzoo.uucp> I wrote:
>>	char const *s, *t;	=is=>  {char const *} s, t
>>	char *const s, *t;	=is=>  char {const *s}, *t
>>
>... (Both declare "t" to be pointer to const char.)

Oops.  Talk about blunders.  I got the important part, about "s", right,
but botched "t".  The first declaration declares t to be pointer to const
char.  The second declares it to be pointer to char.
-- 
Welcome to Mars!  Your         |     Henry Spencer at U of Toronto Zoology
passport and visa, comrade?    | uunet!attcan!utzoo!henry henry@zoo.toronto.edu