I have always had trouble correctly interpreting complex type
declarations without using the "declarator parser" program given in
K&R. Then someone taught me about the "Clockwise" Rule. Start with the
declared item's name and "spiral outwards" in a clockwise direction.
With this rule I was able to guess the type correctly about half the
time (which was still a great improvement).
I recently came across an old (June 1987) article in the C User's Group
Newsletter by Andrew Binstock. I learned that I was not applying the
"Clockwise" Rule correctly. So here it goes:
Take any declaration, start with the innermost parantheses (in the
absence of parantheses start with the declared item's name) and work
clockwise through the declaration GOING TO THE RIGHT FIRST.
It's the GOING TO THE RIGHT FIRST part that I was not applying
correctly (even though I was sprialing outwards in the clockwise
direction), hence the problem. Here are some examples from the
article:
char c; a char
char c[]; an array of char
char *c[]; an array of pointers to chars
char *c(); a function returning a pointer to chars
char* c()[]; a function returning a pointer to an array of chars
int *c()(); a function returning a pointer to a function
returning an int
int (*(*c)[])(); a pointer to an array of pointers to functions
returning an int
In a nutshell:
[] => array of
* => pointer to
(...) => function that returns a/an
Masood