Sub topic 11
Reading complex declarations and function pointers
This chapter assumes you know basic pointers, the array-to-pointer decay rule, and what sizeof returns for arrays and pointers. It teaches you to decode any C declaration mechanically. It also shows how function pointers are declared, called and used. GATE questions rarely ask for more than three levels of nesting, so a reliable procedure matters more than memorising cases.
Why declarations look strange
C declarations follow the rule "declaration mirrors use". The declaration int *p says that the expression *p has type int. The declaration int a[3] says that a[i] has type int. When you combine operators, the same precedence rules that apply to expressions apply to declarators:
- Parentheses
( )for grouping bind tightest. - The postfix operators
[ ](array) and( )(function call) come next. They group left to right. - The prefix
*(pointer) binds more loosely than the postfix operators.
Because [] binds tighter than *, int *p[3] is an array of pointers. You need parentheses to get a pointer to an array.
The reading procedure
Start at the identifier and work outwards. Look right first, then left, and do each parenthesised group before leaving it.
Apply it to int (*a[4])(int):
- The identifier is
a. - Look right:
[4], so "a is an array of 4". - The next token is
), so stop. Look left:*, so "pointers to". - Step out of the parentheses. Look right:
(int), so "functions taking int and returning". - Look left: the base type is
int.
The result: a is an array of 4 pointers to functions that take an int and return an int.
A reference table
| Declaration | Meaning |
|---|---|
| `int *p[3]` | p is an array of 3 pointers to int |
| `int (*p)[3]` | p is a pointer to an array of 3 ints |
| `int *f(int)` | f is a function taking int and returning a pointer to int |
| `int (*f)(int)` | f is a pointer to a function taking int and returning int |
| `int *(*f)(int)` | f is a pointer to a function taking int and returning a pointer to int |
| `int (*a[4])(int)` | a is an array of 4 pointers to functions taking int and returning int |
Pay attention to the difference between int *f(int) and int (*f)(int). The first declares a function. The second declares a variable that can hold the address of a function.
Array of pointers versus pointer to array
The two declarations differ in size and in how arithmetic behaves.
#include <stdio.h>
int main(void) {
int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
int (*p)[3] = m;
int *q[3];
q[0] = m[0];
printf("%zu %zu\n", sizeof p, sizeof q);
printf("%d %d\n", (*(p + 1))[2], p[1][0]);
printf("%d\n", q[0][1]);
return 0;
}On a typical 64-bit system, the first line prints 8 24. The pointer p is one pointer. The array q holds three pointers. The second line prints 6 4. Adding 1 to p moves it by one whole row, which is 3 * sizeof(int) bytes, so p + 1 points to {4, 5, 6}. The third line prints 2.
Function pointers
A function name used in an expression decays to a pointer to that function. This is similar to how an array name decays. So fp = add and fp = &add are equivalent. The call fp(2, 3) and the call (*fp)(2, 3) are also equivalent.
A common use is a dispatch table, which is an array of function pointers indexed by an operation code.
#include <stdio.h>
int add(int x, int y) { return x + y; }
int sub(int x, int y) { return x - y; }
int mul(int x, int y) { return x * y; }
int main(void) {
int (*ops[3])(int, int) = {add, sub, mul};
for (int i = 0; i < 3; i++) {
printf("%d ", ops[i](6, 3));
}
printf("\n");
return 0;
}The output is 9 3 18. The type of ops is exactly the a from the earlier walkthrough, with a different argument list.
Function pointers also let you pass behaviour into another function. The standard qsort takes a comparator of type int (*)(const void *, const void *). The caller supplies the comparison logic.
Simplifying with typedef
A function that returns a function pointer is hard to read when written directly. A typedef names the pointer type once and keeps later declarations short.
#include <stdio.h>
int add(int x, int y) { return x + y; }
int sub(int x, int y) { return x - y; }
typedef int (*binop)(int, int);
binop pick(char c) {
return c == '+' ? add : sub;
}
int (*pick2(char c))(int, int) {
return c == '+' ? add : sub;
}
int main(void) {
printf("%d %d\n", pick('+')(4, 2), pick2('-')(4, 2));
return 0;
}The output is 6 2. Both pick and pick2 are functions that take a char and return a pointer to a function taking two ints and returning an int. Read pick2 with the procedure. The identifier is pick2, and its parameter list (char c) comes first, so it is a function. Its result is the * pointer to the outer (int, int) function, which returns int. The typedef form says the same thing in one line.
const placement
The const qualifier applies to whatever is immediately to its left. If nothing is to its left, it applies to the type on its right.
| Declaration | What is constant |
|---|---|
| `const int *p` | The int, so `*p = 1` is an error and `p = &x` is allowed |
| `int *const p` | The pointer, so `p = &x` is an error and `*p = 1` is allowed |
| `const int *const p` | Both the pointer and the int |
Read these with the same procedure. In int *const p, you read leftwards from p and meet const first ("p is a constant"), then * ("pointer to"), then int.
Common errors
- Writing
int *f(int)when you meant a function pointer. This declares a function and gives no variable to assign to. - Forgetting the parentheses around
*fpwhen calling through a pointer written with explicit dereference.*fp(2, 3)callsfpfirst and then dereferences the result. - Doing arithmetic on function pointers. It is not defined, because functions have no size to step by.
- Assuming
sizeofof a pointer to an array equals the array size. It is the size of one pointer. - Calling through a function pointer whose type does not match the actual function. This is undefined behaviour.
Exercise
Decode the declaration int (*(*g)(void))[5].
Show answer
The identifier is g. Inside the innermost parentheses, *g means g is a pointer. Stepping out, (void) means g points to a function taking no arguments. The next * means that function returns a pointer. The outer [5] means the pointer points to an array of 5. The base type is int. So g is a pointer to a function taking no arguments and returning a pointer to an array of 5 ints.
Exercise
Given int m[2][3] and int (*p)[3] = m;, with sizeof(int) equal to 4, by how many bytes does p + 1 differ from p? What does *(p + 1) point to?
Show answer
p + 1 is 12 bytes beyond p, because the pointed-to type is int[3], which is 3 4 = 12 bytes. The expression `(p + 1) is the second row m[1], an array of 3 ints that decays to a pointer to m[1][0]`.