K&R C function pointers
last modified December 5, 2026 K&R C 1978
A function in C is not a variable, but its address can be taken, stored in a pointer, passed to other functions, and kept in arrays. Calling a function through a pointer lets a program decide at run time which function to call. This article declares function pointers, passes them to a sort, builds a table of functions, and then reads the declarations that combine pointers, arrays, and functions.
Pointers to functions
The name of a function, written without parentheses, gives its address. A
pointer that can hold it is declared with the same mnemonic as other
pointers: int (*fp)(); says that (*fp)() is an
int, so fp is a pointer to a function returning
int.
#include <stdio.h>
int square(x)
int x;
{
return x * x;
}
int cube(x)
int x;
{
return x * x * x;
}
int main()
{
int (*fp)(); /* pointer to a function returning int */
fp = square;
printf("%d\n", (*fp)(4));
fp = cube;
printf("%d\n", (*fp)(4));
return 0;
}
fp = square stores the address of square, and
(*fp)(4) calls the function fp points to. The
parentheses around *fp are required in both places: without
them, int *fp(); would declare a function returning a pointer
to int. The declaration says nothing about the parameters, so
the pointer can hold any function returning int. A function
whose address is taken before its definition must be declared first, for
example with int square();.
$ gcc -std=c89 -pedantic -Wall -Wextra -o fp_basic fp_basic.c $ ./fp_basic 16 64
Calling without the star
The call through a pointer is written (*fp)(x) in K&R C:
the indirection gives the function, and the parentheses call it. A shorter
form appears in later code.
#include <stdio.h>
int twice(x)
int x;
{
return 2 * x;
}
int main()
{
int (*fp)();
fp = twice;
printf("%d\n", fp(21)); /* no star */
return 0;
}
GCC accepts fp(21) and calls the function, because the later
standard allowed the shorter form.
$ gcc -std=c89 -pedantic -Wall -Wextra -o fp_call fp_call.c $ ./fp_call 42
The PDP-11 compiler follows the book: fp is a pointer, not a
function, and it refuses to call it.
$ cc fp_call.c fp_call.c:14: Call of non-function
Functions as arguments
A function pointer can be passed to another function like any other argument. The receiving function declares the parameter as a pointer to a function and calls through it. One function can then perform different operations.
#include <stdio.h>
int square(x)
int x;
{
return x * x;
}
int negate(x)
int x;
{
return -x;
}
/* replace every element v[i] with f(v[i]) */
int apply(v, n, f)
int v[];
int n;
int (*f)();
{
int i;
for (i = 0; i < n; i++)
v[i] = (*f)(v[i]);
return 0;
}
int show(v, n)
int v[];
int n;
{
int i;
for (i = 0; i < n; i++)
printf("%d ", v[i]);
printf("\n");
return 0;
}
int main()
{
static int a[] = { 1, 2, 3, 4, 5 };
apply(a, 5, square);
show(a, 5);
apply(a, 5, negate);
show(a, 5);
return 0;
}
apply calls f on every element of the array.
The parameter is declared int (*f)(); in the old-style
declaration list. The call apply(a, 5, square) passes the
address of square; no & is written, because a
function name already stands for its address.
$ gcc -std=c89 -pedantic -Wall -Wextra -o apply apply.c $ ./apply 1 4 9 16 25 -1 -4 -9 -16 -25
Sorting with a comparison function
A sort must compare elements, but the order is a separate decision. When the comparison is a function passed by the caller, the same sort can order the data in any way.
#include <stdio.h>
int ascending(a, b)
int a, b;
{
return a - b;
}
int descending(a, b)
int a, b;
{
return b - a;
}
int by_size(a, b) /* compare absolute values */
int a, b;
{
if (a < 0)
a = -a;
if (b < 0)
b = -b;
return a - b;
}
/* insertion sort; cmp decides the order */
int sort(v, n, cmp)
int v[];
int n;
int (*cmp)();
{
int i, j, t;
for (i = 1; i < n; i++)
for (j = i; j > 0 && (*cmp)(v[j - 1], v[j]) > 0; j--) {
t = v[j];
v[j] = v[j - 1];
v[j - 1] = t;
}
return 0;
}
int main()
{
static int a[] = { 7, -12, 3, 25, -1, 0, -8 };
int i;
sort(a, 7, ascending);
for (i = 0; i < 7; i++)
printf("%d ", a[i]);
printf("\n");
sort(a, 7, descending);
for (i = 0; i < 7; i++)
printf("%d ", a[i]);
printf("\n");
sort(a, 7, by_size);
for (i = 0; i < 7; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
sort is an insertion sort: it moves each element to the left
while the comparison says that its left neighbour is greater. A comparison
function returns a negative number, zero, or a positive number, as
str_cmp did in the strings
article. descending swaps its operands, and
by_size compares absolute values, which keeps 0 first and puts
-8 before -12. Subtracting two int values is safe here; with
values near the limits of the type it would overflow. The
sorting article develops faster sorts and the
library's qsort, which takes a comparison function in the same
way.
$ gcc -std=c89 -pedantic -Wall -Wextra -o sort_by sort_by.c $ ./sort_by -12 -8 -1 0 3 7 25 25 7 3 0 -1 -8 -12 0 -1 3 7 -8 -12 25
Dispatch tables
An array of function pointers maps a number or a character to an action.
Such a dispatch table replaces a long switch, and
adding an operation means adding an entry.
#include <stdio.h>
int add(a, b) int a, b; { return a + b; }
int sub(a, b) int a, b; { return a - b; }
int mul(a, b) int a, b; { return a * b; }
int quot(a, b) int a, b; { return a / b; }
/* parallel tables: the symbol and the function for it */
char symbols[] = "+-*/";
int (*ops[])() = { add, sub, mul, quot };
/* return the function for operator c, or 0 */
int (*lookup(c))()
int c;
{
int i;
for (i = 0; symbols[i] != '\0'; i++)
if (symbols[i] == c)
return ops[i];
return 0;
}
int main()
{
int i;
int (*f)();
for (i = 0; i < 4; i++)
printf("17 %c 5 = %d\n", symbols[i], (*ops[i])(17, 5));
f = lookup('*');
if (f != 0)
printf("6 * 7 = %d\n", (*f)(6, 7));
if (lookup('%') == 0)
printf("no operator %%\n");
return 0;
}
The four short functions are written on single lines to keep the table
together. ops is an array of pointers to functions returning
int, initialized with the function names, and
symbols holds the matching operator characters at the same
positions. (*ops[i])(17, 5) calls the function in element
i. lookup returns a function pointer, or 0 for an
unknown operator; its declaration is explained in the next section. The
calculator article dispatches operators with
a switch instead.
$ gcc -std=c89 -pedantic -Wall -Wextra -o dispatch dispatch.c $ ./dispatch 17 + 5 = 22 17 - 5 = 12 17 * 5 = 85 17 / 5 = 3 6 * 7 = 42 no operator %
Reading declarations
A declaration in C is written like the expression that uses it, and the
operators in it have their usual precedence: () and
[] bind more tightly than *. A declaration is read
from the name outwards, the parts on the right first, then the
* on the left, with parentheses overriding the order.
| Declaration | Meaning |
|---|---|
int *p; | pointer to int |
int *f(); | function returning a pointer to int |
int (*fp)(); | pointer to a function returning int |
int *a[3]; | array of 3 pointers to int |
int (*row)[4]; | pointer to an array of 4 int |
int (*ops[4])(); | array of 4 pointers to functions returning int |
int (*lookup())(); | function returning a pointer to a function returning int |
The last line is the lookup of the previous section. In its
old-style definition the parameter names go inside the inner parentheses,
int (*lookup(c))(), and the declaration of c
follows. The next program uses three of these declarators.
#include <stdio.h>
int grid[3][4] = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
int first(v) int *v; { return v[0]; }
int last(v) int *v; { return v[3]; }
int main()
{
int (*row)[4]; /* pointer to an array of 4 ints */
int *cells[3]; /* array of 3 pointers to int */
int (*pick[2])(); /* array of 2 pointers to functions */
int i;
pick[0] = first;
pick[1] = last;
for (row = grid; row < grid + 3; row++)
printf("%d..%d ", (*pick[0])(*row), (*pick[1])(*row));
printf("\n");
for (i = 0; i < 3; i++)
cells[i] = &grid[i][i];
printf("%d %d %d\n", *cells[0], *cells[1], *cells[2]);
printf("sizes: %d %d %d\n", (int) sizeof row,
(int) sizeof *row, (int) sizeof cells);
return 0;
}
row points to a whole row of grid, so
row++ moves to the next row and *row is the row,
an array that turns into a pointer to its first element when passed to
first and last. cells holds three
pointers to the diagonal elements, and pick two functions. The
sizes show the difference: row is a pointer, *row
an array of four int, and cells three pointers.
$ gcc -std=c89 -pedantic -Wall -Wextra -o declarators declarators.c $ ./declarators 1..4 5..8 9..12 1 6 11 sizes: 8 16 24
Source
Dennis Ritchie: C Reference Manual (1975)
GCC: Options to Request or Suppress Warnings
In this article we have used function pointers in K&R C: declaring
them, calling with (*fp)(), passing functions as arguments, a
sort with a comparison function, dispatch tables, functions that return
function pointers, and reading complex declarations. The next part of the
series starts with K&R C structures.
Author
List all K&R C tutorials.