Function Pointers in C — NULL Crash in Embedded Firmware
Missing config flag leaves function pointer NULL, triggering hard fault at 0x00000000.
- Function pointers store the address of a function, not the function itself
- Syntax:
int (ptr)(int, int)— parentheses aroundptrare mandatory - Use
typedefto make function pointer types readable - Callbacks allow passing a function as an argument — used in
qsort, event handlers - Dispatch tables replace long switch/if-else chains with an array of function pointers
- Biggest pitfall: calling a NULL function pointer causes a segfault — always check before calling
Imagine you hire a contractor and instead of telling them exactly how to do every step, you hand them a card with a phone number to call when they finish a task — whoever answers decides what happens next. A function pointer is that phone number. It's not the function itself; it's the address where the function lives in memory, so you can hand it to someone else and say 'call this when you're ready.' This lets your code be flexible — the same contractor can call your plumber, your electrician, or your interior designer depending on what card you give them.
Every non-trivial C program eventually hits a wall: you need a piece of code to behave differently depending on context, but you don't want to litter your logic with a dozen if-else branches. Sort algorithms need comparison logic. Event loops need to dispatch to different handlers. Plugin systems need to call code that didn't exist when the core was compiled. In every one of these cases, function pointers are the tool C gives you — and understanding them is what separates C programmers who write clever hacks from those who write clean, extensible systems.
The problem function pointers solve is simple: in C, functions are not first-class values you can pass around the way you'd pass an integer. But their addresses are. A function pointer stores that address, which means you can store a function in a variable, pass it as an argument, return it from another function, or keep a whole table of them. This is how the C standard library's qsort works, how operating system kernels register interrupt handlers, and how game engines implement entity behavior without a class hierarchy.
By the end of this article you'll be able to declare and call function pointers without second-guessing the syntax, build a working callback system, construct a dispatch table that replaces a cascade of if-else statements, and spot the two errors that burn every developer the first time they use function pointers in production code.
Declaring and Calling a Function Pointer — Getting the Syntax Right Once and For All
The syntax for function pointers trips people up because the asterisk belongs to the name, not the return type. Read the declaration from the inside out: the name of the variable is in the middle, wrapped in parentheses with an asterisk, and the surrounding parts describe what function signature it can point to.
For a function that takes two ints and returns an int, the pointer type is: int (operation)(int, int). That parentheses around operation is mandatory — without it, int operation(int, int) is a completely different thing: a function named operation that returns int .
Once you have the pointer, calling it is straightforward. Modern C allows you to call it directly as operation(a, b) — the compiler knows it's a pointer and handles the dereference. The older explicit-dereference syntax (*operation)(a, b) also works and makes the pointer nature more obvious. Both styles are valid; pick one and be consistent within a codebase.
typedef int (*MathOperation)(int, int) once means every subsequent use is just MathOperation. When this type appears in a struct, a parameter list, and a return type — and it will — you'll be very glad you did. It also makes the code self-documenting: MathOperation communicates intent far better than the raw pointer syntax.ptr(args) or (*ptr)(args) — be consistent.Callbacks — Passing Functions as Arguments the Way qsort Does
A callback is nothing more than a function pointer you pass to another function so that function can call yours at the right moment. It's the foundational pattern behind event-driven systems, custom sorting, plugin architectures, and async I/O notification.
The C standard library's qsort is the example every C programmer meets first. You hand qsort your array and a comparator — a function pointer that tells qsort how to decide which of two elements is 'less than' the other. qsort doesn't care what you're sorting or how you define order; it just calls your comparator whenever it needs to compare two elements.
Building your own callback-based API follows the same pattern. You design a function that accepts a function pointer parameter. Callers provide different functions to customize behavior. Your core logic stays untouched.
Dispatch Tables — Replacing if-else Chains With an Array of Function Pointers
Once you can store a function pointer in a variable, you can store them in an array. An array of function pointers is called a dispatch table (or jump table), and it's one of the most useful patterns in systems programming.
Consider a simple calculator that handles four operations. The naive approach is a chain of if-else or a switch statement. That works for four operations, but what about forty? You'd have forty branches, and adding a new operation means touching the dispatcher every single time.
A dispatch table maps an index (or enum value) directly to a function. Adding a new operation is adding one entry to the table. The dispatcher doesn't change at all.
Function Pointers in Structs — How C Fakes Object-Oriented Design
If you put function pointers inside a struct, the struct gains behavior — it's not just data anymore. This is exactly how C++ implements virtual functions under the hood (the vtable).
This pattern works by defining a struct that holds function pointer fields. Different 'instances' can point their pointers at different implementations. Code that operates on the struct calls the function through the pointer without knowing which implementation it's talking to.
This matters because C is used where C++ isn't an option — microcontrollers and kernels. Knowing how to build a clean interface with function pointers lets you write reusable C code rather than copying logic for every new variant.
State Machines with Function Pointers — Clean Event Handling Without Switch Statements
State machines are everywhere — protocol handlers, UI navigation, game states. The textbook approach uses a switch statement with a state variable. That works, but as states grow, the switch becomes a maintenance nightmare. Function pointers offer a cleaner alternative: each state is a function, and the state machine's current state pointer is a function pointer. Transitioning to a new state is as simple as reassigning the pointer.
This pattern is common in embedded systems where memory is tight and you need deterministic timing. Each state function receives events and returns the next state function pointer. The main loop simply calls the current state function in a tight loop, consuming no additional stack depth.
Implementing this avoids the risk of missing a state in a switch, keeps state logic isolated, and makes adding new states trivial — just write a new function and register it.
NULL Function Pointer in Embedded Firmware
if (handler) handler(); else default_handler();.- Always initialize function pointers to a safe default, not just NULL.
- Add a NULL guard before every function pointer call — it costs one branch and prevents hard-to-diagnose crashes.
- Treat uninitialized function pointers as undefined behavior — they will crash eventually.
print ptr — if 0x0, look for missing initialization. Use a conditional breakpoint to catch the assignment.typedef and compile with -Werror to catch mismatches.volatile on the pointer or disable optimization for that file. Alternatively, check that the pointer is not optimized away.if (my_func_ptr) my_func_ptr();Key takeaways
int (op)(int, int) is a pointer to a function; int op(int, int) is a function that returns a pointer.Common mistakes to avoid
3 patternsMissing parentheses around the pointer name in declaration
int handler(int) instead of int (handler)(int). The compiler treats it as a function returning an int pointer, not a pointer to a function. The code compiles but calling handler(5) or dereferencing incorrectly causes undefined behavior.(*name) to ensure the asterisk modifies the variable name, not the return type. Use a typedef to avoid repeating the complex syntax.Calling a NULL function pointer
if (ptr) ptr(args);. Consider providing a default no-op function for empty states.Signature mismatch when casting function pointers
void ()(int) function into a void ()(float) pointer via a cast. The code may compile with a warning, but calling it corrupts the stack because the arguments are interpreted differently.Interview Questions on This Topic
What is the difference between `void *f(int)` and `void (*f)(int)`?
f is a function taking an int and returning a void pointer. The second is a pointer to a function: f is a variable that can hold the address of a function taking an int and returning void. In the second case, you need to assign an actual function before calling f(5).Frequently Asked Questions
That's C Basics. Mark it forged?
4 min read · try the examples if you haven't