How to Read Nasty C/C++Function Pointers

One of the joys of C/C++ is pointers. However, reading complicated C/C++ definitions and function pointers can be a nightmare. Here are some steps to help you read C/C++ pointers and function pointers.

Start at the variable name (or innermost construct if no identifier is present). Look right without jumping over a right parenthesis; say what you see. Look left again without jumping over a parenthesis; say what you see. Jump out a level of parentheses if any. Look right; say what you see. Look left; say what you see. Continue in this manner until you say the variable type or return type.

int *foo[100];
foo is an array of hundred of pointers to int

int (*(*fp)[])();
fp is a pointer to an array of pointers to functions that return ints.

char *( *(**fp [][100]) () )[];
fp is an array of an array of 100 of pointers to pointers to functions that return an array of pointers to char

void (*fp[10]) (void (*)() )
fp is an array of 10 pointers to functions that return void that take a pointer to a function that returns a void as a parameter.

Declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
char *(*(*a[N])())()

Back C++ home | Home