How to Use Function Pointers in Embedded C: Syntax and Examples
In embedded C, use
function pointers to store addresses of functions and call them indirectly. Declare a pointer with the function's signature, assign it to a function, and invoke it like a normal function using the pointer.Syntax
A function pointer stores the address of a function with a specific signature. You declare it by specifying the return type, pointer name in parentheses with an asterisk, and parameter types.
- Return type: The type the function returns (e.g.,
void,int). - Pointer name: The name of the function pointer, enclosed in parentheses with an asterisk (e.g.,
(*ptr)). - Parameters: The types of parameters the function accepts (e.g.,
int,void).
Assign the pointer to a function by using the function name without parentheses. Call the function through the pointer using (*ptr)(args) or simply ptr(args).
c
return_type (*pointer_name)(parameter_types); // Example: void (*func_ptr)(int); // Assigning: func_ptr = some_function; // Calling: func_ptr(5);
Example
This example shows how to declare a function pointer, assign it to different functions, and call them. It simulates a simple embedded system task switcher.
c
#include <stdio.h> // Two simple functions matching the pointer signature void led_on(int pin) { printf("LED on pin %d is ON\n", pin); } void led_off(int pin) { printf("LED on pin %d is OFF\n", pin); } int main() { // Declare a function pointer for functions taking int and returning void void (*led_control)(int); // Assign pointer to led_on and call led_control = led_on; led_control(3); // Turns LED on pin 3 ON // Assign pointer to led_off and call led_control = led_off; led_control(3); // Turns LED on pin 3 OFF return 0; }
Output
LED on pin 3 is ON
LED on pin 3 is OFF
Common Pitfalls
- Wrong pointer type: The function pointer must exactly match the function's return type and parameters.
- Uninitialized pointer: Using a function pointer before assigning it causes crashes.
- Incorrect call syntax: Calling a function pointer without parentheses or with wrong arguments leads to errors.
- Forgetting &: You can assign a function name directly without
&, but mixing styles can confuse readers.
c
// Wrong: pointer type mismatch // void (*ptr)(int); // ptr = some_function_returning_int; // Correct: int some_function(int x) { return x; } int (*ptr)(int) = some_function; // Wrong: calling uninitialized pointer // void (*ptr)(int); // ptr(5); // Crash // Correct: void func(int x) { } void (*ptr)(int) = func; ptr(5);
Quick Reference
Remember these tips when using function pointers in embedded C:
- Match the function pointer signature exactly to the target function.
- Always initialize function pointers before use.
- Use
ptr(args)or(*ptr)(args)to call functions. - Function names can be assigned directly without
&. - Function pointers enable flexible callbacks and interrupt handling in embedded systems.
Key Takeaways
Declare function pointers with the exact function signature to avoid errors.
Assign function pointers before calling to prevent crashes.
Call functions through pointers using parentheses like normal functions.
Function pointers enable flexible and modular embedded C code.
Avoid mismatched types and uninitialized pointers for safe embedded programming.