C Function Pointer
Function Pointer
A function pointer is like a normal pointer, but instead of pointing to a variable, it points to a function.
This means it stores the address of a function, allowing you to call that function using the pointer.
Function pointers are useful when you want to decide which function to run while the program is running, or when you want to pass a function as an argument to another function.
Think of it like saving a phone number - the pointer knows where the function lives in memory, so you can "call" it later.
Function Pointer Example
Let's start with a complete example. Below it, you will learn about each part:
Example
A pointer to a function that adds two numbers:
int add(int a, int b) {
return a + b;
}
int main() {
int (*ptr)(int, int) = add;
int result = ptr(5, 3);
printf("Result: %d", result);
return 0;
}
Here:
ptris a pointer to the functionadd().ptr(5, 3)calls the function using the pointer.
This works exactly the same as calling add(5, 3).
Note: A function name itself points to the start of its code in memory. That means a function name already acts like a pointer! Declaring a function pointer just gives you a variable that can hold that address - so you can change it or pass it around.
Declaring a Function Pointer
The general syntax for declaring a function pointer is:
returnType (*pointerName)(parameterType1, parameterType2, ...);
For example:
int (*ptr)(int, int);
This means ptr is a pointer (*ptr) to a function that takes two int values (int, int) and returns an int.
The parentheses around *ptr are important. Without them, int *ptr(int, int); would declare a function that returns a pointer, not a pointer to a function.
Assigning a Function to a Pointer
You can assign a function to a pointer in two ways:
ptr = add;
ptr = &add;
Both are the same, because the function's name already represents its address in memory.
Calling a Function Through a Pointer
Once the pointer is assigned, you can call the function in two ways:
ptr(5, 3);
(*ptr)(5, 3);
Both are valid and do the same thing.
Passing a Function as an Argument
You can pass a function pointer to another function. The function that is passed is called a callback function.
It allows a function to call another function that you provide as input.
Example
Passing a function pointer to another function:
void greetMorning() { printf("Good morning!\n"); }
void greetEvening() { printf("Good evening!\n"); }
void greet(void (*func)()) {
func();
}
int main() {
greet(greetMorning);
greet(greetEvening);
return 0;
}
Here, greet() takes another function as a parameter and calls it.
This way, the same function can do different things, depending on which function you give it.
The parameter void (*func)() is a pointer to a function that takes no parameters and returns nothing (void).
Function Pointer Array
You can also store several function pointers in an array, so you can choose which function to run while the program is running.
This example runs three different functions using an array of function pointers:
Example
void add() { printf("Add\n"); }
void subtract() { printf("Subtract\n"); }
void multiply() { printf("Multiply\n"); }
int main() {
void (*operations[3])() = { add, subtract, multiply };
for (int i = 0; i < 3; i++) {
operations[i]();
}
return 0;
}
operations is an array of 3 pointers to functions that take no parameters and return nothing.
This is often used for simple menus, command lists, or calculators - anywhere you want to call different functions based on user input.
Example
A simple calculator using an array of function pointers:
void add(int a, int b) { printf("Result: %d\n", a + b); }
void subtract(int a, int b) { printf("Result: %d\n", a - b); }
void multiply(int a, int b) { printf("Result: %d\n", a * b); }
int main() {
int choice, x = 10, y = 5;
void (*operations[3])(int, int) = { add, subtract, multiply };
printf("x = %d, y = %d\n\n", x, y);
printf("Choose an operation:\n");
printf("0: Add\n1: Subtract\n2: Multiply\n");
scanf("%d", &choice);
if (choice >= 0 && choice < 3) {
operations[choice](x, y);
} else {
printf("Invalid choice!\n");
}
return 0;
}
In this program:
- All three math functions (
add(),subtract(), andmultiply()) are stored in an array. - The user picks which one to run by entering a number.
- The correct function is then called using the pointer array.
This is a simple example of how function pointer arrays make programs more flexible and reusable.
Function Pointer vs Normal Function
| Normal Function | Function Pointer |
|---|---|
| Called directly by its name | Called using a pointer |
| The function is decided before the program runs | You can choose which function to call while the program is running |
| Good for simple code | Good for flexible and reusable code |
Summary
- A function pointer stores the address of a function.
- You can declare, assign, and call functions through it.
- It allows passing functions as arguments to other functions.
- Useful for callbacks, menus, and flexible program design.