Function Pointers
A funtcion pointer stores the memory address of the entry point of a function. This allows functions to be passed as arguments or stored in data structures for dynamic execution.
int multiply(int a, int b) {
return a * b;
}
int main() {
// Declaration: return_type (*pointer_name)(parameter_types)
int (*op_ptr)(int, int) = multiply; // The function name acts as its address
int val1 = 10, val2 = 20;
// Invoking the function via the pointer
int result = op_ptr(val1, val2);
printf("Address of multiply: %p\n", (void*)multiply);
printf("Value from pointer: %d\n", result);
return 0;
}
Distinguishing int (*p)(int, int) from int *p(int, int) is critical. The former is a pointer to a function, while the latter is a function declaration that returns a pointer to an integer.
Arrays of Function Pointers
An array of function pointers can store multiple functions with the same signature. This is often used to implement dispatch tables or state machines.
void task_alpha() { printf("Executing Alpha\n"); }
void task_beta() { printf("Executing Beta\n"); }
void task_gamma() { printf("Executing Gamma\n"); }
int main() {
// Array of pointers to functions taking no arguments and returning void
void (*tasks[])() = { task_alpha, task_beta, task_gamma };
for (int i = 0; i < 3; i++) {
tasks[i]();
}
return 0;
}
Practical Implementation: A Dispatch Table Calculator
Using an array of function pointers allows for a clean implementation of a menu-driven calculator, avoiding repetitive conditional blocks.
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }
int div_op(int a, int b) { return b != 0 ? a / b : 0; }
void show_menu() {
printf("1: Add | 2: Sub | 3: Mul | 4: Div | 0: Exit\n");
}
int main() {
// The index aligns with the menu choices
int (*ops[])(int, int) = { NULL, add, sub, mul, div_op };
int choice, x, y;
while (1) {
show_menu();
printf("Selection: ");
if (scanf("%d", &choice) != 1) break;
if (choice == 0) break;
if (choice >= 1 && choice <= 4) {
printf("Enter two operands: ");
scanf("%d %d", &x, &y);
printf("Result: %d\n", ops[choice](x, y));
} else {
printf("Invalid selection.\n");
}
}
return 0;
}
Pointers to Function Pointer Arrays
Just as we can have pointers to standard arrays, we can define a pointer that points to an entire array of function pointers. This is a higher-level abstraction in C memory management.
int compute(int a, int b) { return a + b; }
int main() {
// Array of 3 function pointers
int (*func_arr[3])(int, int) = { compute, compute, compute };
// Pointer to the array defined above
int (*(*ptr_to_arr)[3])(int, int) = &func_arr;
// Usage through dereferencing the array pointer
int result = (*ptr_to_arr)[0](10, 20);
return 0;
}
Callback Functions
A callback function is a function passed to another functon as an argument. The receiving function "calls back" the passed function when a specific event occurs or a task needs to be performed.
int square(int n) { return n * n; }
int cube(int n) { return n * n * n; }
// apply_transformation accepts a function pointer as a callback
void apply_transformation(int input, int (*callback)(int)) {
int result = callback(input);
printf("Input: %d, Result: %d\n", input, result);
}
int main() {
// Passing different logic into the same processing function
apply_transformation(5, square);
apply_transformation(3, cube);
return 0;
}