I've come across two pointer declarations that I'm having trouble understanding. My understanding of precedence rules goes something like this:
Operator Precedence Associativity
(), [ ] 1 Left to Right
*, identifier 2 Right to Left
Data type 3
But even given this, I can't seem to figure out how to evaluate the following examples correctly:
First example
float * (* (*ptr)(int))(double **,char c)
My evaluation:
*(ptr)
(int)
*(*ptr)(int)
*(*(*ptr)(int))
Then,
double **
char c
Second example
unsigned **( * (*ptr) [5] ) (char const *,int *)
*(ptr)
[5]
*(*ptr)[5]
*(*(*ptr)[5])
**(*(*ptr)[5])
How should I read them?
Best Answer
My guess for the first one: ptr is a pointer to a function that takes as parameter an int, and returns a pointer to a function that takes as parameters a pointer to pointer to double and a char, and returns a pointer to float.
Interpretation:
(*ptr)(int)
says that ptr is a pointer to a function taking an int as an argument. To discover what that function returns we need to expand our view:
(* (*ptr)(int))
this means the function returns a pointer to another function. The parameters of that other function are:
(double **,char c)
and it returns
float *
And for the second one: ptr is a pointer to an array of five pointers to functions that take as parameters a constant pointer to char and a pointer to int, returning a pointer to a pointer of unsigned int.
Interpretation:
( * (*ptr) [5] )
declares ptr as a pointer to array of five pointers to a function taking
(char const *,int *)
as arguments and returning
unsigned **