Function Pointers in C++: A Practical Example
Function pointers in C++ are not just theoretical constructs; they are powerful tools that greatly enhance the flexibility and dynamism of your code. Let's delve into a real-world scenario where we utilize an array of function pointers for performing various mathematical operations!
💡 Imagine the Scenario: Dynamic Mathematical Operations
Imagine you have a task that requires performing different mathematical operations based on user input or changing requirements. Instead of relying on convoluted conditional statements, we can implement an elegant solution using an array of function pointers.
🛠️ Technical Insight: Declaration, Initialization, Invocation
Declaration
Function pointers are declared with a syntax that mirrors the signature of the functions they point to.
void (*funcPtr)(int, float);
Initialization Function pointers are initialized by assigning the address of a compatible function.
funcPtr = &someFunction;
Invocation Invoking a function through a function pointer is as straightforward as calling a regular function.
funcPtr(42, 3.14);
In this example, someFunction is a function that takes an integer and a float as arguments, and funcPtr is a function pointer that points to someFunction. When funcPtr is invoked with arguments 42 and 3.14, it calls someFunction with those arguments.
implementation

Function Pointers in C
The same concept exists in C, where function pointers power callbacks like qsort. The declaration needs parentheses around the pointer name — without them, int *compare(...) would be parsed as a function returning a pointer.
#include <stdio.h>
#include <stdlib.h>
int ascending(const void *a, const void *b) {
int x = *(const int *)a;
int y = *(const int *)b;
return x - y;
}
int main(void) {
int values[] = { 42, 7, 19, 3 };
size_t count = sizeof(values) / sizeof(values[0]);
qsort(values, count, sizeof(values[0]), ascending);
for (size_t i = 0; i < count; i++) {
printf("%d ", values[i]);
}
return 0;
}
ascending is a function pointer passed to qsort, which calls it for every pair of elements. Swapping the comparator for descending changes the sort order without touching the sorting code — the same dynamic behavior as the C++ array-of-function-pointers example above.
Output

This simple yet powerful mechanism allows for dynamic and flexible behavior in your C++ code, making it easier to adapt to changing requirements and user inputs.
FAQ
What is a function pointer in C?
A function pointer is a variable that stores the address of a function. The declaration mirrors the function signature, for example int (*compare)(const void *, const void *), and it is called exactly like a normal function.
Why use function pointers instead of long if/else chains? A function pointer lets you choose behavior at runtime — pass a comparator, callback, or handler — without editing the code that uses it. That keeps the call site small and makes new behaviors additive.
Can the same code work in C and C++?
Yes. Function pointer syntax is compatible; C++ adds std::function, lambdas, and member function pointers, but the C-style form still works for plain functions and libraries such as qsort.