TITLE: rules of thumb for reading declarations (Newsgroups: comp.lang.c++.moderated,comp.lang.c++, 17 Sep 98) JER: "JER" > > Please give me the meaning of the declaration: > int ( * t (int) )( int * ); HYSLOP: jim.hyslop@leitch.com The rules for parsing are: Start at the identifier. Process to the right until you hit the end of declaration, or a closing parenthesis. Process to the left until you hit an opening parenthesis, or the beginning of the declaration. Repeat until the declaration is fully parsed. So, the declaration is read as: int ( * t (int) )( int * ) ; 10 6 5 1 2 3 4 7 8 9 11 1 - t 2 - is a function 3 - which accepts an int parameter 4 - [closing parenthesis - parse backwards now] 5 - and returns a pointer 6 - [opening parenthesis - parse forwards now] 7 - to a function 8 - which accepts an int * 9 - [closing parenthesis - parse backwards now] [corrected typo -adc] 10- and returns an int [beginning of dec - parse forwards now] 11- end of declaration - you're done. So: "t is a function which accepts an int parameter and returns a pointer to a function which accepts an int * and returns an integer". The following code will work with your declaration: #include using std::cout; using std::endl; int x(int *val) { cout << "In x, passed value of " << *val << endl; return *val; } int y(int *val) { cout << "In y, passed value of " << *val << endl; return *val; } int z(int *val) { cout << "In z, passed value of " << *val << endl; return *val; } int ( * t (int v) )( int *) // Your original declaration { // Exercise to the reader - how do you parse arr: // Hint: "=" is the end-of-declaration static int (*arr[])(int *) = {x, y, z}; if (v < 0 || v >= sizeof(arr) / sizeof(arr[0])) { return NULL; } return arr[v]; } int main(void) { // and how do you parse: int(*p) (int *); int count=0; p=t(count); while(p) { cout << "p(&count) returned " << p(&count) << endl; p=t(++count); } } HTH I seem to recall writing a program which would read a declaration and print out its meaning, i.e. given input of "int ( * t (int) )( int * );" the output would be "t is a function which ..." - this may be a useful exercise for learning how to parse declarations!