Is `char const *foo;' legal?

Michael Meissner meissner at osf.org
Fri Jan 12 02:41:48 AEST 1990


In article <25ABBF93.9618 at paris.ics.uci.edu> rfg at 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;
| 
| It treats such a declaration as being identical to:
| 
| 	const char *foo;
| 
| Now I have looked at the draft ANSI standard, and I can find no examples
| that look like `<typename> const *<variable-name>;'.
| 
| Due to the fact that the C declaration syntax is so complex, I don't
| even want to try to think about the issue of whether or not such declarations
| are syntactically legal.
| 
| So let me just ask the general question: "Are such declarations both
| syntactically and semantically legal?"

Yes.  The reason you didn't see syntax for:

	<typename> const *<variable-name>;

is because it would lead to a reduce/reduce conflict, since const and
volatile are typenames.

| Please excuse me if this question seems excessively naive.  It is just
| that I have never before seen any declarations of this form.
| 
| 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.
| 
| I have found no basis in the standard for either this binding *or* for the
| other alternative (i.e. binding the `const' more tightly than the `[]').
| Did I miss something?  Which binding is "correct"?  Why?

In the above example, const binds with char (ie, lower priority),
since the typename goes to the innermost part of the declaration.
--
Michael Meissner	email: meissner at osf.org		phone: 617-621-8861
Open Software Foundation, 11 Cambridge Center, Cambridge, MA

Catproof is an oxymoron, Childproof is nearly so



More information about the Comp.std.c mailing list