Francis said:
I'm trying to understand the 6.7.5 section of C99 which is about
"Declarators" but I'm quite confused and has hard time to understand
what's written.
For example I'm trying to retrieve the type of 'foo':
I know it's basically an array of array of int, but I'd like to
retrieve this by using the spec.
I may need to use 6.7.5.2.p3 but I don't understand what it's written
in it, specially the "derived-declarator-type-list T" thing.
What 6.7.5.2p3 says is:
> If, in the declaration ‘‘T D1’’, D1 has one of the forms:
> D[ type-qualifier-listopt assignment-expressionopt ]
> D[ static type-qualifier-listopt assignment-expression ]
> D[ type-qualifier-list static assignment-expression ]
> D[ type-qualifier-listopt * ]
> and the type specified for ident in the declaration ‘‘T D’’ is
> ‘‘derived-declarator-type-list T’’, then the type specified for ident
> is ‘‘derived-declarator-type-list array of T’’
Now, the first thing you have to understand is that the meaning of
"ident" is explained in 6.7.5p4:
> In the following subclauses, consider a declaration
> T D1
> where T contains the declaration specifiers that specify a type T
> (such as int) and D1 is a declarator that contains an identifier
> ident.
The second thing to understand is that derived-declarator-type-list is
just a placeholder, whose meaning is actually defined by the sentence
where it is used. What that sentence means is that
derived-declarator-type-list has whatever meaning it needs to have in
order to make "derived-declarator-type-list T" a correct description of
the type specified for ident by a declaration of the form "T D".
The tricky part is that we have to apply 6.7.5.2p3 twice:
1. T D1 is int foo[4][8]
T is int
D1 is foo[4][8]
D is foo[4]
ident is foo
Now, derived-declarator-type-list has whatever value it needs to have in
order to say that the type declared for "foo" by the declaration "int
foo[4]" is "derived-declarator-type-list int". However, before we can
identify that type, we have to apply 6.7.5.2p3 a second time:
2. T D1 is int foo[4]
T is int
D1 is foo[4]
D is foo
ident is foo
Now, derived-declarator-type-list has whatever value it needs to have in
order to say that the type declared for "foo" by the declaration "int
foo" must match "derived-declarator-type-list int". But the type
declared for "foo" by "int foo" is "int", so for this case
derived-declarator-type-list is ""
Therefore, applying 6.7.5.2p3, we find that the type declared by "int
foo[4]" is "array of int". applying 6.7.5.2p4, we find it is more
specifically "array with length 4 of int", though 6.7.5.2p4 doesn't
specify exactly how the specification of the length should be worded. I
had to experiment with several wordings to come up with one that sounded
vaguely like proper English.
1'. Going back to the first application of 6.7.5.2p3,
"derived-declarator-type-list int" must match "array with length 4 of
int", which means that
derived-declarator-type-list is "array with length 4 of"
Applying 6.7.5.2, we find that the type declared by "int foo[4][8]" is
"array with length 4 of array of int". Applying 6.7.5.2p4, it is more
specifically "array with length 4 of array with length 8 of int".
I hope that explanation clarifies things?