Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!rice!uw-beaver!uw-entropy!quick!srg From: srg@quick.COM (Spencer Garrett) Newsgroups: comp.std.c Subject: Re: Is `char const *foo;' legal? Message-ID: <7612@quick.COM> Date: 11 Jan 90 10:40:08 GMT References: <25ABBF93.9618@paris.ics.uci.edu> Organization: Quicksilver Engineering, Seattle Lines: 45 In article <25ABBF93.9618@paris.ics.uci.edu>, rfg@paris.ics.uci.edu (Ron Guilmette) writes: > I have recently learned that the GNU C compiler accepts the following > declaration without complaint: > > char const *foo; > ... > So let me just ask the general question: "Are such declarations both > syntactically and semantically legal?" > ... > One other question. If this form of declaration *is* legal, then > does the standard contain any verbage which would clarify the type of > `bar' in the following example? > > void foo (char const bar[]) > { > } > > GCC accepts this declaration, and it binds the `const' with lower `priority' > that the `[]'. Thus, the type of `bar' is taken as pointer to constant char. Not only legal, but IMHO preferred. Think of "const" and "volatile" not as type names, but as *operators* (on a par with *) with the unusual characteristic of being allowed to float all the way to the left of the type names and storage-class indicators (which sometimes makes the declaration read better (in English)). Parentheses aside, they modify the declaration to their right, just like *. I prefer to write: char const *foo; to emphasize that "const *foo" is a char. When you take the * off to look at the named variable itself you have taken off the const keyword as well, so you know that the pointer isn't constant, just the chars. In the same way, you read char *bar[]; as an array of pointers, so char const bar[]; would be an array of const chars. Use in a parameter declaration implicitly turns "array of" into "pointer to" in either case. I hope this helps.