Is `char const *foo;' legal?

Steve Emmerson steve at groucho.ucar.edu
Fri Jan 12 02:29:08 AEST 1990


Ron Guilmette, <rfg at paris.ics.uci.edu>, writes with regard to the following 
declaration:

>	char const *foo;

>So let me just ask the general question: "Are such declarations both
>syntactically and semantically legal?"

I cannot speak on the standard since I don't have one.  K&R-II, however
indicates that the above is syntactically and semantically valid.  The
above string can be generated by the following sequence (cf. appendix A13):

	declaration

	declaration-specifiers init-declarator-list(opt) ";"

	storate-class-specifier declaration-specifiers(opt) init-declarator ";"

	"char" type-qualifier declaration-specifiers(opt) declarator ";"

	"char" "const" pointer(opt) direct-declarator ";"

	"char" "const" "*" type-qualifier-list(opt) identifier ";"

	"char" "const" "*" "foo" ";"

The crucial grammar rule is

	declaration-specifiers:
		storage-class-specifier declaration-specifiers(opt)
		type-specifier declaration-specifiers(opt)
		type-qualifier declaration-specifiers(opt)

which allows generation of both "const char" and "char const".

Section A8.6.1 says that type-qualifies which follow the "*" in a pointer
declaration apply to the pointer itself, rather than to the pointed-at
object.  Previous statements and examples about type-qualifiers -- in
which they were placed before the "*" -- indicate that they apply to the
pointed-at objects.

[As an aside, it might be a good idea to obtain the book -- if you
haven't already.  Zot!]

Steve Emmerson        steve at unidata.ucar.edu        ...!ncar!unidata!steve



More information about the Comp.std.c mailing list