In the following declarations, p is ...
| int *p; | pointer to int |
| int *p[10]; | array[10] of pointers to int |
| int (*p)[10]; | pointer to array[10] of int |
| int *p( ); | function returning pointer to int |
| int (*p)( ); | pointer to function returning int |
| int *(*p)( ); | pointer to function returning pointer to int |
| int (*p[ ])( ); | array[ ] of pointers to function returning int |
| int (*(*p( ))[5])( ); | function returning pointer to array[5] of pointers to function returning int |
The above become quite clear when we consider the precedence and associativity of operators:
( ) [ ] {left to right}
* {right to left}
As an example what is p in the declaration:
int *((*(*p[5])( )))[10];
One nice and easy way to come to the correct conclusion is by assuming that you are the compiler and going through the following obvious steps. Obvious when you keep in mind the precedence and associativity of the operators at hand that is [], ( ), *.
First we have p[5] which is obviously an array, so we have array[5] of
Next is *p[5], which is a pointer, thus array[5] of pointers to
Then (*p[5])(), which represents a function, hence array[5] of pointers to function returning
Then *(*p[5])(), a pointer hence array[5] of pointers to function returning pointer to
Next consider ((*(*p[5])()))[10], which is an array, so we get array[5] of pointers to function returning pointer to array[10] of (an extra pair of ( ) above is of no use although it does no harm)
Then comes *((*(*p[5])()))[10], which is a pointer, so we get array[5] of pointers to function returning pointer to array[10] of pointers to
Lastly, the type which is int in this case which ultimately leads us to array[5] of pointers to function returning pointer to array[10] of pointers to int
When you are writing code all you have to do is to follow the reverse of the above steps, which is fairly easy.
Such a complicated (?? if you have gone through the above, it should seem too simple now
本文详细解析了C语言中复杂的指针、数组及函数声明,并通过分解语法结构来帮助理解其含义。

2179

被折叠的 条评论
为什么被折叠?



