Is `char const *foo;' legal?

Walter Murray walter at hpclwjm.HP.COM
Sat Jan 13 06:37:47 AEST 1990


[Regarding a declaration like "const char bar[]"]
 
[Description of how GCC interprets this]

>| 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 this example, "bar" has type "array of const char".  The const qualifier
applies to the element type, not the array type.

Here are relevant sections from the Standard (quoting from the 12/7/88
draft):

The type of an array is defined by 3.5.4.2, page 68, lines 6-10.
The inductive method used to define the type of a derived declarator
can be a little confusing.  See the paragraph in 3.5.4 beginning on 
page 66, line 45, for an explanation.

Section 3.1.2.5, page 25, lines 22-23:  "A derived type is not
qualified by the qualifiers (if any) of the type from which it
is derived."

If you try to use a typedef to force the array type itself to be
qualified, it won't work.  Section 3.5.3, page 65, lines 23-25.

Walter Murray
----------



More information about the Comp.std.c mailing list