Index
A pointer to a function in C allows you to store the address of a function and then call the function indirectly through the pointer. Function pointers are particularly useful in scenarios such as callback functions, implementing function tables, or when you need to pass functions as arguments to other functions.
1. Declaring a Function Pointer
The syntax for declaring a function pointer is:
return_type (*pointer_name)(parameter_types);
For example, if we have a function int add(int, int)
, a pointer to this function would be declared as:
int (*funcPtr)(int, int);
2. Assigning a Function to a Function Pointer
You can assign the address of a function to a function pointer using the function’s name (without parentheses).
int add(int a, int b) {
return a + b;
}
int (*funcPtr)(int, int); // Declare a function pointer
funcPtr = add; // Assign the address of `add` to `funcPtr`
Now, funcPtr
can be used to call add
.
3. Calling a Function via a Pointer
Once you have assigned a function to a pointer, you can call the function using the pointer like this:
int result = funcPtr(5, 3); // Calls add(5, 3) and stores the result
Example of Function Pointer
Here’s an example that demonstrates how to use a function pointer to call different arithmetic operations based on the pointer’s assignment:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int main() {
int (*operation)(int, int); // Declare a function pointer
// Assign `add` to the function pointer and call it
operation = add;
printf("Addition: %d\n", operation(5, 3));
// Assign `subtract` to the function pointer and call it
operation = subtract;
printf("Subtraction: %d\n", operation(5, 3));
return 0;
}
Explanation:
- We declare a function pointer
operation
that can point to functions taking twoint
arguments and returning anint
. - First, we set
operation
to point toadd
and calloperation(5, 3)
, which returns8
. - Then, we set
operation
to point tosubtract
and calloperation(5, 3)
, which returns2
.
4. Passing Function Pointers as Arguments
Function pointers can also be passed as arguments to other functions. This is useful in scenarios where a function’s behavior depends on the specific operation passed to it.
Example of Passing Function Pointers as Arguments
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int multiply(int a, int b) {
return a * b;
}
void performOperation(int x, int y, int (*operation)(int, int)) {
printf("Result: %d\n", operation(x, y));
}
int main() {
// Call performOperation with different function pointers
performOperation(5, 3, add); // Calls add(5, 3)
performOperation(5, 3, multiply); // Calls multiply(5, 3)
return 0;
}
Explanation:
performOperation
takes two integers and a function pointer as parameters.- By passing different functions (
add
ormultiply
) toperformOperation
, we can dynamically choose the operation to perform onx
andy
.
Summary
- Function pointers store addresses of functions, allowing indirect function calls.
- Function pointers can be assigned to different functions and called like normal functions.
- You can pass function pointers as arguments to other functions, allowing flexibility in function behavior.