Master C Function Pointers and Callbacks: Concepts, Syntax, and Real‑World Examples
This article explains the fundamentals of C function pointers, how to declare and use them, employ typedef for readability, pass them as arguments, return them from functions, store them in arrays, and apply them to implement flexible callback mechanisms with complete code examples.
1. What Is a Function Pointer?
A function pointer is a variable that stores the address of a function, allowing the function to be called indirectly. Its declaration follows the pattern return_type (*pointer_name)(parameter_types); where the parentheses around the pointer name are mandatory to distinguish it from a regular function declaration.
2. Declaring Function Pointers with typedef
Using typedef simplifies the syntax: typedef int (*Fun1)(int); Examples:
typedef int (*Fun1)(int); typedef int (*Fun2)(int, int); typedef void (*Fun3)(void); typedef void* (*Fun4)(void*);3. Calling a Function Through a Pointer
Assign the function’s name (or its address) to the pointer and invoke it:
int Func(int x); int (*p)(int); p = Func; // or p = &Func int result = (*p)(5); // can also be written as p(5)A complete example demonstrates reading two integers, assigning Max to a pointer, and calling it via the pointer.
4. Using Function Pointers as Parameters
Define a function‑pointer type and pass a function to another function:
typedef void (*FunType)(int); void callFun(FunType fp, int x) { fp(x); } void myFun(int x) { printf("myFun: %d
", x); } int main() { callFun(myFun, 100); return 0; }5. Function Pointers as Return Types
A function can return a pointer to another function:
void (*func5(int, int, float))(int, int) { /* ... */ }The returned pointer can then be used to invoke the target function.
6. Arrays of Function Pointers
Since a function pointer is a pointer, it can be stored in an array:
void (*func_array_1[5])(int, int, float); typedef void (*p_func_array)(int, int, float); p_func_array func_array_2[5];7. Summary of Function Pointer Rules
Function‑pointer constant: Max ; variable: p . Both (*myFun)(10) and myFun(10) are valid; the latter is preferred for readability. Function pointers can be stored in arrays, e.g., int (*fArray[10])(int);
8. What Is a Callback Function?
A callback is a function invoked through a function pointer passed as an argument to another function. It enables the caller to remain agnostic of the concrete implementation, only requiring a matching signature.
9. Why Use Callbacks?
Callbacks decouple the caller from the callee, allowing flexible substitution of behavior without changing the caller’s code.
10. Implementing Callbacks in C
Define several callback functions and a handler that receives a function pointer:
int Callback_1(int a) { printf("Hello, Callback_1: a = %d
", a); return 0; } int Handle(int x, int (*Callback)(int)) { Callback(x); return 0; } int main() { Handle(4, Callback_1); /* … */ return 0; }This demonstrates that passing the function name supplies its address, making callbacks a specific use case of function pointers.
11. Callback Example: Arithmetic Operations
A struct holds pointers to basic arithmetic functions, which can be called directly or via a generic library function:
typedef struct { float (*p_add)(float,float); /* … */ } OP; float ADD(float a,float b){ return a+b; } /* SUB, MUL, DIV */ void init_op(OP *op){ op->p_add = ADD; /* … */ } float add_sub_mul_div(float a,float b,float (*op_func)(float,float)){ return (*op_func)(a,b); }The program prints results using both direct pointer calls and the generic callback function.
12. Real‑World Callback Use: State‑Machine for GPRS Module
A state‑machine array stores status codes and corresponding handler functions. The engine iterates the array, finds the matching status, and invokes the associated callback, illustrating how callbacks simplify complex workflows.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
