int *a[10]
declare a
as an array of 10 pointers to int
.
int (*a)[10]
declare a
as a pointer to an array of 10 int
s.
Remember these two rules to decipher pointer declarations:
Always read declarations from inside out: Start from innermost, if any, parenthesis. Locate the identifier that's being declared, and start deciphering the declaration from there.
When there is a choice, always favour [] and () over *
: If *
precedes the identifier and []
follows it, the identifier represents an array, not a pointer. Likewise, if *
precedes the identifier and ()
follows it, the identifier represents a function, not a pointer. (Parentheses can always be used to override the normal priority of []
and ()
over *
).
For example:
int *a[10]; "a is"
^
int *a[10]; "a is an array of 10"
^^^^
int *a[10]; "a is an array of 10 pointers"
^
int *a[10]; "a is an array of 10 pointers to `int`".
^^^