C Function Pointers
A function pointer is a variable that stores the address of a function rather than a data value.
If you need to work with functions dynamically in C, function pointers allow you to store and invoke functions using their addresses. They are useful for callbacks, event handling, and implementing flexible code architectures.
Understanding Function Pointers
The syntax for declaring a function pointer is:
return_type (*pointer_name)(parameter_types);
Explanation:
return_type
: The type of value the function returns.(*pointer_name)
: The name of the function pointer variable; the asterisk indicates it is a pointer.(parameter_types)
: A comma-separated list of the parameter types the function accepts.
Example 1: Basic Function Pointer Usage
This example demonstrates how to create a function pointer to a simple function that adds two integers and then call the function through the pointer.
main.c
#include <stdio.h>
// A simple function that adds two integers
int add(int a, int b) {
return a + b;
}
int main() {
// Declare a function pointer that points to a function taking two ints and returning an int
int (*funcPtr)(int, int) = add;
// Call the function using the function pointer
int result = funcPtr(10, 20);
printf("Result of add(10, 20): %d\n", result);
return 0;
}
Explanation:
int add(int a, int b)
: Defines a function that adds two integers.int (*funcPtr)(int, int) = add;
: Declares a function pointer namedfuncPtr
that points to a function with the same signature asadd
, and initializes it with the address ofadd
.funcPtr(10, 20)
: Calls theadd
function via the function pointer with arguments 10 and 20.printf()
: Prints the result returned by the function call.
Output:
Result of add(10, 20): 30
Example 2: Using an Array of Function Pointers
This example shows how to store multiple functions in an array of function pointers and invoke them based on their index. This technique is useful when you want to select a function to execute at runtime.
main.c
#include <stdio.h>
// Function definitions
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
int main() {
// Array of function pointers
int (*operations[3])(int, int) = { add, subtract, multiply };
int x = 15, y = 5;
// Call each function using the function pointer array
printf("Add: %d\n", operations[0](x, y));
printf("Subtract: %d\n", operations[1](x, y));
printf("Multiply: %d\n", operations[2](x, y));
return 0;
}
Explanation:
- Three functions (
add
,subtract
, andmultiply
) are defined, each taking two integers and returning an integer. - An array
operations
of function pointers is declared; each element is capable of pointing to a function that matches the signature(int, int) -> int
. - The array is initialized with the addresses of the three functions.
- The functions are invoked by calling the corresponding element in the array with the arguments
x
andy
. printf()
prints the results of each function call.
Output:
Add: 20
Subtract: 10
Multiply: 75
Conclusion
In this tutorial, we learned about C function pointers, including:
- What Function Pointers Are: Variables that store the address of functions.
- Basic Usage: Declaring, initializing, and calling a function using a pointer.
- Array of Function Pointers: Organizing multiple function pointers in an array to dynamically choose which function to execute.