In C language, functions are also a type of “object”, which have addresses in memory. Therefore, it is possible to define pointers to functions, which can be used for dynamic calls, callback handling, building function tables, etc.
Mastering function pointers is key to understanding the “low-level abstraction” and “modular programming” in C language.
1. Basic Concept of Function Pointers
A function pointer is a variable used to store the address of a function. Through it, one can indirectly call a function just like calling a regular function.
Example: Regular Function
int add(int a, int b) {
return a + b;
}
The function name <span>add</span> is actually the address of the function in memory.
Function Pointer Definition Syntax
ReturnType (*FunctionPointerName)(ParameterType1, ParameterType2, ...);
Example: Definition and Assignment
int (*fp)(int, int); // Define pointer
fp = add; // Alternatively, write as fp = &add;
int result = fp(2, 3); // Indirectly call the function
2. Use Cases of Function Pointers
| Scenario | Description |
| Callback Mechanism | Register functions for asynchronous calls by other code |
| Strategy Selection | Select one from multiple processing methods |
| Interface Abstraction | Encapsulate module details |
| Table-Driven Programming | Build an array of functions to replace a large number of if/else statements |
3. Array of Function Pointers
Used to store a group of function addresses for quick lookup calls.
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int (*ops[2])(int, int) = {add, sub};
Call:
printf("%d\n", ops[0](5, 3)); // Output 8
printf("%d\n", ops[1](5, 3)); // Output 2
4. Function Pointers as Parameters (Callback Functions)
Pass function pointers as parameters to implement callbacks for user-defined logic within functions.
void compute(int x, int y, int (*op)(int, int)) {
printf("Result: %d\n", op(x, y));
}
Call:
compute(10, 5, add); // Output 15
compute(10, 5, sub); // Output 5
5. Using typedef to Simplify Function Pointer Declarations
The syntax for function pointers can be complex, and using <span>typedef</span> can improve readability.
typedef int (*Operation)(int, int);
Operation op = add;
printf("%d\n", op(1, 2)); // Output 3
Also applicable for parameter definitions:
void compute(int x, int y, Operation f) {
printf("%d\n", f(x, y));
}
6. Functions Returning Function Pointers (Higher-Order Functions)
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
typedef int (*OpFunc)(int, int);
OpFunc getOp(char ch) {
if (ch == '+') return add;
else return sub;
}
int main() {
OpFunc f = getOp('+');
printf("%d\n", f(3, 2)); // Output 5
}
7. Implementing a Menu System Using Function Pointers (Practical Example)
#include <stdio.h>
void show() { printf("Show\n"); }
void insert() { printf("Insert\n"); }
void del() { printf("Delete\n"); }
void (*menu[])(void) = {show, insert, del};
int main() {
int choice;
while (1) {
printf("0: Show, 1: Insert, 2: Delete\n");
scanf("%d", &choice);
if (choice >= 0 && choice < 3)
menu[choice]();
else
break;
}
return 0;
}
8. Function Pointers vs General Pointers
| Feature | General Pointer (e.g., int*) | Function Pointer |
| Type Pointing | Data address (variables, arrays, etc.) | Function address |
| Assignment Method | <span>p = &a</span> or <span>p = arr</span> |
<span>fp = func</span> or <span>fp = &func</span> |
| Calling Method | <span>*p</span> |
<span>fp(x, y)</span> |
| Application Field | Data access | Callbacks, strategies, polymorphism |
9. Common Errors and Precautions
| Error Type | Example | Correct Method |
| Forgot to Add Parentheses | <span>int *f(int,int);</span> |
<span>int (*f)(int,int);</span> |
| Parameter List Mismatch | <span>int (*f)(int);</span> called with <span>f(1,2)</span> |
Ensure parameter types and counts match |
| Calling Without Initialization | <span>int (*f)(int,int); f(1,2);</span> |
Ensure pointer is valid before calling |
| Misusing Array Index Out of Range | <span>fp[3]();</span><span> out of array bounds</span> |
Control index to avoid overflow |
10. Practical Significance of Function Pointers
- • Improve code flexibility and scalability;
- • Simulate the “polymorphism” mechanism of object-oriented programming;
- • Widely used in drivers, GUI libraries, and game engines.
11. Summary
| Content | Description |
| Define Function Pointer | <span>int (*f)(int,int)</span> |
| Pointer Array | <span>int (*arr[])(int,int)</span> |
| Callback Function | Pass function pointer as parameter |
| typedef Simplifies Declaration | <span>typedef int (*Op)(int,int)</span> |
| Higher-Order Function | Return function pointer, build strategy functions |
| Function Pointer Uses | Callback mechanisms, function tables, strategy patterns, etc. |