Function Pointers
Storing and calling functions through pointers
🎯 What are Function Pointers?
Function pointers store addresses of functions, allowing dynamic function calls and creating flexible, reusable code with callback mechanisms and function arrays.
// Function pointer declaration and usage
int add(int a, int b) { return a + b; }
int (*operation)(int, int) = add;
int result = operation(5, 3); // Calls add function
Function Pointer Concepts
Declaration
Declaring function pointers
int (*ptr)(int, int);
Assignment
Pointing to functions
ptr = &function_name;
Function Call
Calling through pointer
result = (*ptr)(arg1, arg2);
Function Arrays
Arrays of function pointers
int (*ops[])(int, int);
🔹 Basic Function Pointer Example
Function pointers are variables that store memory addresses of functions instead of data. They enable you to call functions dynamically at runtime, making your code more flexible and modular. To declare a function pointer, specify the return type, pointer symbol, and parameter types: int (*ptr)(int, int). This syntax allows functions to be passed as arguments, stored in arrays, and invoked through the pointer. Function pointers are essential for creating callback mechanisms and implementing polymorphic behavior in C.
#include <stdio.h>
// Function definitions
int add(int a, int b) {
return a + b;
}
int multiply(int a, int b) {
return a * b;
}
int main() {
// Function pointer declaration
int (*operation)(int, int);
// Point to add function
operation = add;
printf("Addition: %d\n", operation(10, 5));
// Point to multiply function
operation = multiply;
printf("Multiplication: %d\n", operation(10, 5));
return 0;
}
Output:
Addition: 15 Multiplication: 50
🔹 Array of Function Pointers
Function pointer arrays allow you to create a calculator or dispatch system using multiple function references. By storing function pointers in an array, you can select and execute different operations based on user input or index values. This technique eliminates lengthy switch-case statements and makes code maintenance easier. For example, int (*ops[])(int, int) = {add, subtract, multiply} creates an array of operation functions. Arrays of function pointers are commonly used in event handlers, command dispatchers, and mathematical computation systems.
#include <stdio.h>
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 divide(int a, int b) { return b != 0 ? a / b : 0; }
int main() {
// Array of function pointers
int (*operations[])(int, int) = {add, subtract, multiply, divide};
char symbols[] = {'+', '-', '*', '/'};
int a = 20, b = 4;
printf("Calculator using function pointers:\n");
for (int i = 0; i < 4; i++) {
int result = operations[i](a, b);
printf("%d %c %d = %d\n", a, symbols[i], b, result);
}
return 0;
}
Output:
Calculator using function pointers: 20 + 4 = 24 20 - 4 = 16 20 * 4 = 80 20 / 4 = 5
🔹 Callback Functions
Callback functions are powerful mechanisms for handling asynchronous operations and event-driven programming in C. By passing a function pointer to another function, you allow that function to execute your code at specific moments. Callbacks enable functions to notify you when operations complete, errors occur, or events happen. This pattern is widely used in signal handlers, sorting custom comparators, and graphical user interfaces. Understanding callbacks is essential for building responsive and modular applications that react to runtime events.
#include <stdio.h>
// Callback functions
void onSuccess() {
printf("Operation completed successfully!\n");
}
void onError() {
printf("Operation failed!\n");
}
// Function that accepts callback
void processData(int data, void (*callback)()) {
printf("Processing data: %d\n", data);
if (data > 0) {
callback = onSuccess;
} else {
callback = onError;
}
callback(); // Call the callback function
}
int main() {
processData(100, NULL);
processData(-5, NULL);
return 0;
}
Output:
Processing data: 100 Operation completed successfully! Processing data: -5 Operation failed!