问题
So I want to give an example:
int *pi; // pi is a pointer that points to an integer
const int *cpi; // cpi is a pointer that points to a constant integer
char *pc; // pc is a pointer to a char
How can I read these:
char **x; //x is a pointer to a char pointer?
char *y[];
char **z[];
Thanks.
回答1:
cdecl.org is often linked to such questions. No doubt that it make easier to decipher any complex declaration, but at the same time it just provide an abstracted information. Being a C or C++ programmer one should know how to decipher complex declaration manually. Spiral Rule help to some extent but fails in some cases. This answer will help programmers to decipher any complex declaration manually.
Remember these two simple rules:
- Always read declaration from the inside out.
- When there is a choice, always favor
[]
and()
over*
.
The first rule simply states that, locate the variable that is being declared and start deciphering the declaration from it.
For second rule, if *
precedes the identifier and []
or ()
follows it, then the identifier represents an array or function (respectively), not a pointer.
Example 1:
char *y[5];
- Variable/identifier is
y
. *
precedesy
and follows[]
.y
must be an array.
Combining above deciphering will result in: y
is an array of 5
pointers to char
.
Also note that you can always use parentheses to override the normal priority of []
or ()
.
Example 2:
void (*pf) (int);
- Variable/identifier is
pf
. *pf
is enclosed in parenthesis, it must be a pointer.()
follows*pf
, meanspf
must points to a function.- Since
()
enclosesint
, function must expects an argument of typeint
.
So, pf
is a pointer to function that expects an int
argument and returns nothing.
Now, what would you get after deciphering the following declaration
int *(*a[5])(void);
?
Answer:
a
is an array of pointers to functions that expects no argument and returning pointer toint
.
Note: Note that both of
char *y[];
char **z[];
will cause compilation error if they are not declared as arguments of a function. If they are function's argument then char *y[]
is equivalent to char **y
and char **z[]
is equivalent to char ***z
.
If that's not the case, then you need to specify the dimension as I did in my first example.
回答2:
Some example C declarations taken from the HELPPC utility (by David Jurgens) that saved my (programming) life back in the nineties (online version of the utility here: http://stanislavs.org/helppc )
int i; i as an int
int *i; i as a pointer to an int
int **i; i is a pointer to a pointer to an int
int *(*i)(); i is a pointer to a function returning a
pointer to int
int *(*i[])(); i is an array of pointers to functions
returning pointers to an int
int *i[5]; i is an array of 5 pointers to int
int (*i)[5]; i is a pointer to an array of 5 ints
int *i(); i is a function returning a pointer to an int
int (*i)(); i is a pointer to a function returning int
int *(*(*i)())[5] i is a pointer to a function returning a
pointer to an array of 5 pointers to an int
回答3:
y
is an array of pointers to char, z
is an array of pointers to pointer to char. x
is a pointer to pointer to char
回答4:
char **x; //x is a pointer to a pointer to char
char *y[]; // y is an array of pointers to char
char **z[]; // z is an array of pointers to pointers to char
来源:https://stackoverflow.com/questions/29591245/how-to-decipher-complex-pointer-declarations-in-c