The catch here is that modifier precedence matters. The array modifier has higher precedence than the pointer modifier. Thus, [ ] is applied to name and an array is declared. This results in an array of size length.
The declaration could be modified with the use of parenthesis, to be 'type (*name)[length]'. In this case, the pointer modifier is applied first, and therefore a pointer is declared.
There is a quite good explanation at
http://msdn2.microsoft.com/en-us/library/1x82y1z4(VS.80).aspx which I will try to summarize in the following lines.
*) Start with the identifier
*) Go to the right first: brackets have higher precedence that asterisks.
*) Whenever you hit a parenthesis, solve the parenthesis as a unit.
When you have to solve, for instance, the type of an argument, start
from the innermost parenthesis (this is from the second article I mention below).
*) Last, apply the type specifier.
Then, for 'type *name[length]':
The identifier name is declared as
(right) an array of size length of
(left) pointers to
(type specifier) type
For 'type (*name)[length]':
The identifier name is declared as
(right, hit parenthesis, solve as a unit) a pointer to
(right) an array of size length of
(type specifier) type
Parenthesis (not shown in my example) for specifying functions are subject to the same rules as brackets, have the same precedence and associate from left to right with them (that is, the one most to the left is interpreted first).
There another article worth reading at
http://www.codeproject.com/cpp/complex_declarations.asp
From the last article, I would like to highlight the recommendation of always writing the asterisk next to the identifier and not to the type. If you write 'int* p, q' you could be mislead to think that both p and q are pointers, however only p is. If you write 'int *p, q', there's no such confusion. For both p and q to be pointers, you would have to write 'int *p, *q'.
No comments:
Post a Comment