I happen to come across the following function pointer.
char (*(*x())[])();
It looks like an array of function pointer in the following format, but I can’t see what f -> (*x()) means. How to interpret this messy function pointer?
char (*f[])();
ADDED
With John Bode’s help, I make an example as follows.
#include <stdio.h>
char foo() { return 'a'; }
char bar() { return 'b'; }
char blurga() { return 'c'; }
char bletch() { return 'd'; }
char (*gfunclist[])() = {foo, bar, blurga, bletch};
char (*(*x())[])()
{
static char (*funclist[4])() = {foo, bar, blurga, bletch};
return &funclist;
}
int main()
{
printf("%c\n",gfunclist[0]());
char (*(*fs)[4])();
fs = x();
printf("%c\n",(*fs)[1]());
}
I could get the expected result.
smcho@prosseek temp2> ./a.out a b
And, you can find a better implementation here.
My general procedure is to find the leftmost identifier in the declaration, and then work my way out, remembering that
[]and()bind before*(i.e.,*f()is normally parsed as*(f())and*a[]is normally parsed as*(a[])).So,
What would such a beast look like in practice?
The expression
&funclistwill return a pointer to the array, so