A function pointer is a variable that stores the memory address of an executable function. Since a function resides in the program’s memory space, its name, when used without parentheses, evaluates to its starting address.
1. Declaring a Function Pointer
The syntax for declaring a function pointer is crucial and must match the signature (return type and parameter types) of the function it intends to point to.
Syntax: return_type (*pointer_name)(param_type1, param_type2, ...);
| Component | Description |
return_type | The return type of the function (e.g., int, void). |
(*pointer_name) | The pointer variable name, which must be enclosed in parentheses. |
(param_types) | The types of the function’s parameters. |
Example: Declaration
Let’s assume we have these functions:
int add(int a, int b);
void greet(char *name);
The corresponding function pointer declarations would be:
// Points to functions that return int and take (int, int)
int (*op_ptr)(int, int);
// Points to functions that return void and take (char *)
void (*hello_ptr)(char *);
2. Initialization and Assignment
A function pointer can be initialized with the name of a function whose signature matches the pointer’s declaration. The address-of operator (&) is optional but often recommended for clarity.
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
// Initialization (using the function name):
int (*op_ptr)(int, int) = add;
// or using the optional address-of operator:
// int (*op_ptr)(int, int) = &add;
// Later assignment:
op_ptr = subtract;
3. Invoking the Function
You can call the function through the pointer using either the explicit dereference method or the cleaner, implied dereference method.
| Method | Syntax | Description |
| Explicit Dereference | (*op_ptr)(5, 3); | Explicitly dereferences the pointer to get the function, then calls it. |
| Implied Dereference | op_ptr(5, 3); | The compiler automatically dereferences the pointer (preferred style). |
// op_ptr currently points to the subtract function
int result = op_ptr(10, 4); // result is 6 (10 - 4)
4. Application: Callback Functions
The most significant use of function pointers is to implement callback mechanisms. A callback is a function passed as an argument to another function, which is then executed (or “called back”) inside the recipient function.
This is essential for functions that perform a generic task but need customization, such as sorting, iterating over a list, or handling asynchronous events.
Example: Simple Calculator with Callback
// 1. Define the function pointer type (using typedef for clarity)
typedef int (*BinaryOpPtr)(int, int);
// 2. A generic function that takes two numbers and a function pointer
int calculator(int a, int b, BinaryOpPtr operation) {
return operation(a, b);
}
// 3. Main execution
int main() {
int x = 10, y = 5;
// Pass the 'add' function as the callback
int sum = calculator(x, y, add);
printf("Sum: %d\n", sum); // Output: 15
// Pass the 'subtract' function as the callback
int diff = calculator(x, y, subtract);
printf("Difference: %d\n", diff); // Output: 5
return 0;
}
