๐งฉ C Function Pointers โ Callback, Syntax, and Examples
๐งฒ Introduction โ What Are Function Pointers?
In C programming, function pointers are pointers that point to the address of a function rather than data. This enables powerful features like callback functions, dynamic execution, and function arrays.
๐ฏ In this guide, youโll learn:
- How function pointers work in C
- Syntax to declare, assign, and call function pointers
- Real-world examples like callbacks and function tables
- Best practices and dangers to avoid
๐ Core Concept โ Function as First-Class Address
In C, functions have addresses just like variables. You can:
- Store a functionโs address in a pointer
- Pass the pointer as an argument
- Call the function using the pointer
โ Basic Syntax:
return_type (*pointer_name)(parameter_list);
๐ป Code Examples โ Function Pointer Usage
โ Example 1: Simple Function Pointer
#include <stdio.h>
void greet() {
printf("Hello from a function pointer!\n");
}
int main() {
void (*func_ptr)() = greet; // Assigning function address
func_ptr(); // Calling function via pointer
return 0;
}
๐จ๏ธ Output:
Hello from a function pointer!
๐ Explanation:func_ptr
holds the address of greet()
. func_ptr()
calls it.
โ Example 2: Pointer to Function with Parameters
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*operation)(int, int) = add;
printf("Sum = %d\n", operation(5, 3));
return 0;
}
๐จ๏ธ Output:
Sum = 8
โ Example 3: Callback Function
#include <stdio.h>
void notify(const char* msg) {
printf("NOTIFY: %s\n", msg);
}
void process(void (*callback)(const char*)) {
callback("Processing complete.");
}
int main() {
process(notify); // Passing function as argument
return 0;
}
๐จ๏ธ Output:
NOTIFY: Processing complete.
๐ Used commonly in libraries and event handlers.
๐ก Best Practices & Tips
๐ Best Practice: Always match the signature exactlyโreturn type and parameters.
๐ก Tip: Use typedef to simplify complex function pointer syntax.
typedef int (*math_op)(int, int);
math_op op = add;
โ ๏ธ Pitfall: Dereferencing incorrect function pointer types leads to undefined behavior.
๐ Function Pointer Use Cases
Use Case | Description |
---|---|
Callback Mechanism | Event handlers, GUI callbacks, comparators |
Dynamic Dispatch | Select function at runtime from user input |
Function Tables/Arrays | Replace switch-case with array of function calls |
Plugin-like Systems | Dynamically register function implementations |
๐ ๏ธ Real-World Applications
qsort()
in standard library uses function pointers- Operating systems (interrupt tables, syscall dispatch)
- GUI frameworks (event handling)
- Game engines (AI strategy switching)
๐ Summary โ Recap & Next Steps
Function pointers give C programmers flexibility to write modular, reusable, and dynamic code. Theyโre crucial for callbacks, dispatch tables, and runtime decision making.
๐ Key Takeaways:
- A function pointer stores a function’s address
- It must match the exact function signature
- Can be passed as argument, returned from function, or stored in arrays
- Ideal for modular, flexible program design
โ๏ธ Real-World Relevance:
Used extensively in drivers, GUI systems, embedded firmware, OS kernels, and more.
โ Frequently Asked Questions (FAQ)
โ What is a function pointer?
โ A variable that holds the address of a function and can be used to invoke it.
โ Can you pass a function as an argument in C?
โ Yes, using function pointers. This enables callbacks and dynamic behaviors.
โ Whatโs the syntax for a pointer to a function returning int
and taking two int
arguments?
โ
int (*ptr)(int, int);
โ How is qsort()
related to function pointers?
โ
qsort()
accepts a comparator function pointer to define sorting behavior dynamically.
โ Can function pointers point to main()
or recursive functions?
โ Technically yes, but doing so is discouraged and can result in undefined behavior depending on context.
Share Now :