Interpreting Declarations

suggest change

A distinctive syntactic peculiarity of C is that declarations mirror the use of the declared object as it would be in a normal expression.

The following set of operators with identical precedence and associativity are reused in declarators, namely:

The above three operators have the following precedence and associativity:

Operator | Relative Precedence | Associativity | ———————–– | —————–– | ————— |[] (array subscription) | 1 | Left-to-right |() (function call) | 1 | Left-to-right |\* (dereference) | 2 | Right-to-left |

When interpreting declarations, one has to start from the identifier outwards and apply the adjacent operators in the correct order as per the above table. Each application of an operator can be substituted with the following English words:

Expression | Interpretation | ———————–– | ————————————————— |thing[X] | an array of size X of… |thing(t1, t2, t3) | a function taking t1, t2, t3 and returning… |*thing | a pointer to… |

It follows that the beginning of the English interpretation will always start with the identifier and will end with the type that stands on the left-hand side of the declaration.


char *names[20];

[] takes precedence over \*, so the interpretation is: names is an array of size 20 of a pointer to char.

char (*place)[10];

In case of using parentheses to override the precedence, the \* is applied first: place is a pointer to an array of size 10 of char.

int fn(long, short);

There is no precedence to worry about here: fn is a function taking long, short and returning int.

int *fn(void);

The () is applied first: fn is a function taking void and returning a pointer to int.

int (*fp)(void);

Overriding the precedence of (): fp is a pointer to a function taking void and returning int.

int arr[5][8];

Multidimensional arrays are not an exception to the rule; the [] operators are applied in left-to-right order according to the associativity in the table: arr is an array of size 5 of an array of size 8 of int.

int **ptr;

The two dereference operators have equal precedence, so the associativity takes effect. The operators are applied in right-to-left order: ptr is a pointer to a pointer to an int.

Multiple Declarations

The comma can be used as a separator (not acting like the comma operator) in order to delimit multiple declarations within a single statement. The following statement contains five declarations:

int fn(void), *ptr, (*fp)(int), arr[10][20], num;

The declared objects in the above example are:

Alternative Interpretation

Because declarations mirror use, a declaration can also be interpreted in terms of the operators that could be applied over the object and the final resulting type of that expression. The type that stands on the left-hand side is the final result that is yielded after applying all operators.

 * Subscripting "arr" and dereferencing it yields a "char" result.
 * Particularly: *arr[5] is of type "char".
char *arr[20];

 * Calling "fn" yields an "int" result.
 * Particularly: fn('b') is of type "int".
int fn(char);

 * Dereferencing "fp" and then calling it yields an "int" result.
 * Particularly: (*fp)() is of type "int".
int (*fp)(void);

 * Subscripting "strings" twice and dereferencing it yields a "char" result.
 * Particularly: *strings[5][15] is of type "char"
char *strings[10][20];

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents