Function Pointers
What is a function pointer?
You already know a pointer can hold the address of a variable. Here is the surprising part: functions also live at addresses in memory, so a pointer can hold the address of a function too. That is a function pointer.
Why would you want this? Because it lets your program decide at run time which function to call, store functions in arrays, and pass behaviour into other functions. It is the foundation of callbacks and flexible, plugin-style code.
The syntax, broken down
The declaration looks strange at first. Read it in three parts:
int (*fp)(int, int); // ^ ^ // | the two parameter types // the pointer name, wrapped in ( ) // "int" is the return type
Read as: "fp is a pointer to a function that takes two ints and returns an int." The parentheses around *fp are what make it a function pointer rather than a function that returns a pointer.
int (*fp)(int) = pointer to a function. int *fp(int) = a function returning int *. Completely different!
Calling a function through a pointer
Point the function pointer at a real function (just use its name), then call it.
#include <stdio.h>
int add(int a, int b) { return a + b; }
int main() {
int (*fp)(int, int) = add; // point at add
printf("Sum = %d\n", fp(3, 4)); // call through fp
return 0;
}Sum = 7
A function's name by itself is its address, so fp = add works directly. Then fp(3, 4) calls add through the pointer.
Callbacks: passing behaviour around
Because a function pointer is just a value, you can pass it into another function. The receiver "calls back" whatever you gave it — this is a callback.
#include <stdio.h>
int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }
void compute(int x, int y, int (*op)(int, int)) {
printf("Result = %d\n", op(x, y)); // call whatever was passed
}
int main() {
compute(4, 5, add); // Result = 9
compute(4, 5, mul); // Result = 20
return 0;
}Result = 9
Result = 20
The same compute function behaves differently depending on which function you hand it. That flexibility is the whole point.
An array of function pointers (menu)
Store several functions in an array and pick one by index — perfect for a menu without a big switch.
#include <stdio.h>
int add(int a, int b){ return a + b; }
int sub(int a, int b){ return a - b; }
int mul(int a, int b){ return a * b; }
int main() {
int (*ops[3])(int, int) = { add, sub, mul };
int choice = 2; // 0=add, 1=sub, 2=mul
printf("Answer = %d\n", ops[choice](6, 3));
return 0;
}Answer = 18
Choosing index 2 calls mul, giving 6 × 3 = 18. Change choice to run a different operation with no if-else at all.
Why function pointers matter
- Callbacks — let libraries call your code (event handlers, comparison functions for sorting).
- Menus / dispatch tables — pick a function by number instead of long switches.
- Plugins — swap behaviour at run time without changing the caller.
Common mistakes
- Forgetting the parentheses around
*fp, changing the meaning entirely. - Making the pointer's signature not match the real function's parameters or return type.
- Calling a function pointer that was never assigned (still NULL or garbage).
- Confusing
fp(the pointer) withfp()(calling through it).
Build a tiny calculator: an array of four function pointers for +, -, ×, ÷, and let the user pick an operation by number. Call the chosen function on two inputs and print the result.
Summary
- A function pointer stores the address of a function, so you can call it indirectly.
- Declare it as
returnType (*name)(paramTypes)— parentheses around*nameare required. - Assign it a function by name, then call with
name(args). - Passing one into another function creates a callback.
- An array of function pointers makes clean menus and dispatch tables.
Frequently Asked Questions
What is a function pointer in C?
How do you declare a function pointer in C?
*, then the parameter types. For example int (*fp)(int, int); declares fp as a pointer to a function that takes two ints and returns an int. The parentheses around *fp are essential.What is a callback in C?
Why do the parentheses matter in a function pointer declaration?
int *fp(int) means "a function returning int *", while int (*fp)(int) means "a pointer to a function returning int". The parentheses around *fp change the meaning completely, so leaving them out is a classic mistake.