3.4 How to decipher a pointer declaration?
Ans Section 5.12, Complicated Declarations, of K&R II says:
"C is sometimes castigated (criticized) for the syntax
of its declaration, particularly ones that involve pointers
to functions. ..."
Following an extract from a book to which the author calls
The Precedence Rule.
The Precedence Rule for Understanding C Declaration:
A Declarations are read by starting with the name and then
reading in precedence order.
B The precedence, from high to low, is:
B.1 parantheses grouping together parts of a declaration
B.2 the postfix operators:
parantheses () indicating a function, and
square brackets [] indicating an array
B.3 the prefix operator: the asterisk denoting
"pointer to".
C If a const and/or volatile keyword is next to a type specifier
(e.g., int, long, etc.) it applies to the type specifier.
Other the const and/or volatile keyword applies to the
pointer asterisk on its immediate _left_.
An example of solving a declaration using the Precedence Rule:
char * const * (*next) ();
RULE EXPLANATION
A First, go the variable name, "next", and note that it is
directly enclosed by the parantheses.
B.1 So we group it with what else is in the parantheses, to
get "next is a pointer to...".
B Then we go outside the parantheses, and have a choice of
a prefix asterisk, or a postfix of parantheses.
B.2 Rule B.2 tells us the highest precedence thing is the
function parantheses at the right, so we have "next is
pointer to a function returning...".
B.3 The process the prefix "*" to get "pointer to".
C Finally, take the "char * const", as constant pointer to
a char.
Then put it all together to read:
"next is a pointer to a function returning a pointer to a
const pointer-to-char"
and we're done.