C - Function Pointers in C
1. What is a Function Pointer?
In C, a function pointer is a pointer that stores the address of a function instead of the address of a variable. Just like a normal pointer points to data (such as int or float), a function pointer points to executable code.
This allows a program to call a function indirectly using the pointer. Function pointers are very useful in advanced programming techniques such as callbacks, event handling, and dynamic function selection.
2. Why Do We Need Function Pointers?
Function pointers are mainly used when:
-
You want to pass a function as an argument to another function.
-
You want to choose a function at runtime.
-
You want to implement callbacks.
-
You want to create flexible and reusable code.
They are commonly used in systems programming, embedded systems, and libraries.
3. Syntax of Function Pointer
The syntax may look complex at first:
return_type (*pointer_name)(parameter_types);
Example:
int (*funcPtr)(int, int);
This means:
-
funcPtris a pointer -
It points to a function
-
The function returns an int
-
The function takes two int parameters
4. Example of Function Pointer
Step 1: Define a function
int add(int a, int b) {
return a + b;
}
Step 2: Declare a function pointer
int (*funcPtr)(int, int);
Step 3: Assign the function address
funcPtr = add;
Step 4: Call the function using pointer
int result = funcPtr(5, 3);
This will call the add function and store the result (8).
5. Passing Function Pointer as Argument
Function pointers are often passed to other functions.
Example:
int multiply(int a, int b) {
return a * b;
}
void calculate(int x, int y, int (*operation)(int, int)) {
printf("Result = %d", operation(x, y));
}
int main() {
calculate(4, 5, multiply);
return 0;
}
Here:
-
calculatereceives a function pointer -
It calls the function using the pointer
-
This makes the program flexible
6. Array of Function Pointers
You can also create an array of function pointers.
Example:
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int main() {
int (*operations[2])(int, int) = {add, subtract};
printf("%d\n", operations[0](10, 5));
printf("%d\n", operations[1](10, 5));
return 0;
}
This allows selecting functions dynamically using an index.
7. Advantages of Function Pointers
-
Makes code more flexible.
-
Helps in implementing callback functions.
-
Reduces repeated code.
-
Useful in menu-driven programs.
-
Used in building libraries and frameworks.
8. Important Points to Remember
-
The function pointer’s signature must exactly match the function.
-
Parentheses around
*pointer_nameare mandatory. -
The function name itself represents its address.
-
You do not need
&while assigning the function name.
9. Real-Life Use Case
In real-world C programming:
-
Sorting functions use function pointers to decide sorting criteria.
-
Operating systems use function pointers for system calls.
-
Embedded systems use them for interrupt handling.
Function pointers may look complicated at first, but with practice, they become a powerful tool in writing dynamic and modular C programs.