Question
I have recently been working with function pointers in C and want a clear summary of the basics.
How do function pointers work in C, what is their syntax, and how are they used in practice? A beginner-friendly explanation with simple examples would be helpful.
Short Answer
By the end of this page, you will understand what a function pointer is in C, how to declare and use one, how to pass it to other functions, and where it appears in real programs such as callbacks, sorting functions, and menu dispatch systems.
Concept
A function pointer is a variable that stores the address of a function.
In C, functions live at memory addresses just like data does. Normally, you call a function directly by its name:
int add(int a, int b) {
return a + b;
}
int result = add(2, 3);
But you can also store the function's address in a pointer and call it indirectly:
int (*operation)(int, int) = add;
int result = operation(2, 3);
This matters because it lets your code become more flexible. Instead of hardcoding one behavior, you can choose a function at runtime.
Common reasons function pointers matter:
- Callbacks: one function lets another function decide what action to perform
- Custom behavior: pass different logic into reusable code
- Dispatch tables: map commands or options to functions
- Library APIs: many C libraries expect callback functions
The important rule is this:
- A function pointer must match the function's return type and parameter types
For example, if a function returns int and takes two int arguments, the pointer must be declared to match that exact signature:
int (*ptr)(int, int);
Function pointers are powerful, but the syntax looks unusual at first because C must distinguish between:
- a function declaration
- a pointer declaration
- a pointer to a function declaration
Once you learn to read the declaration from the variable name outward, the syntax becomes much easier.
Mental Model
Think of a function pointer as a remote control button.
- The function is the device that performs the action
- The function name is like knowing the device directly
- The function pointer is a programmable button that can be set to trigger different devices
For example, suppose you have these functions:
addsubtractmultiply
A function pointer lets you say:
- "Right now, this button means
add" - "Later, change it so the same button means
multiply"
Your code can call the same pointer variable, but the actual function that runs can change.
That is the core idea: the call site stays the same, but the behavior can be swapped.
Syntax and Examples
Basic syntax
Here is a normal function:
int add(int a, int b) {
return a + b;
}
A pointer to a function with the same signature looks like this:
int (*ptr)(int, int);
Read it as:
ptris a pointer*ptris grouped with parentheses- it points to a function
- that takes
(int, int) - and returns
int
Assigning a function to a pointer
int (*ptr)(int, int) = add;
You can also write:
int (*ptr)(int, int) = &add;
Both work in C for this use.
Calling through a function pointer
Step by Step Execution
Consider this example:
#include <stdio.h>
int square(int x) {
return x * x;
}
int main(void) {
int (*func)(int) = square;
int result = func(6);
printf("%d\n", result);
return 0;
}
Step by step:
- The program defines
square, a function that takes oneintand returns oneint. - In
main,funcis declared as a pointer to a function with signatureint (int). func = square;stores the address ofsquareinfunc.func(6)calls the function thatfuncpoints to.
Real World Use Cases
Function pointers appear in many practical C programs.
1. Callbacks in libraries
A library may let you provide your own function to handle events or data.
Examples:
- comparison functions for sorting
- signal handlers
- iteration callbacks
- GUI or embedded event handlers
2. Sorting with custom logic
The C standard library function qsort uses a function pointer so you can decide how elements should be compared.
int compare_ints(const void *a, const void *b) {
int x = *(const int *)a;
int y = *(const int *)b;
return x - y;
}
3. Menu systems
A command-line tool can map user choices to functions.
void show_help(void);
void run_backup(void);
void exit_program();
Real Codebase Usage
In real codebases, developers usually use function pointers in structured ways rather than declaring them casually everywhere.
Common patterns
Callbacks
A function receives another function to customize behavior.
void process_items(int *items, int count, void (*handler)(int)) {
for (int i = 0; i < count; i++) {
handler(items[i]);
}
}
This keeps process_items reusable.
Dispatch tables
Instead of a long chain of if or switch, developers sometimes store functions in an array.
#include <stdio.h>
void say_hello(void) { printf("Hello\n"); }
void say_bye(void) { printf("Bye\n"); }
int {
(*actions[])() = { say_hello, say_bye };
actions[]();
actions[]();
;
}
Common Mistakes
1. Forgetting parentheses in the declaration
This is one of the most common mistakes.
Broken code:
int *ptr(int, int);
This does not mean “pointer to function”. It means:
ptris a function- it takes two
intvalues - it returns
int *
Correct code:
int (*ptr)(int, int);
The parentheses around *ptr are required.
2. Using the wrong function signature
Broken code:
int add(int a, int b) {
return a + b;
}
void (*ptr)(int, int) = add;
Problem:
- returns
Comparisons
Function pointer vs normal function call
| Concept | Normal function call | Function pointer call |
|---|---|---|
| How function is chosen | Fixed at compile time in your code | Can be selected indirectly at runtime |
| Syntax | add(2, 3) | ptr(2, 3) |
| Flexibility | Lower | Higher |
| Readability | Simpler | Slightly harder at first |
Function pointer declaration vs function declaration
| Syntax | Meaning |
|---|---|
int func(int, int); | func is a function returning |
Cheat Sheet
Core syntax
Declare a function:
int add(int a, int b);
Declare a pointer to that kind of function:
int (*ptr)(int, int);
Assign a function:
ptr = add;
Call through the pointer:
ptr(2, 3);
(*ptr)(2, 3);
Reading the declaration
int (*ptr)(int, int);
ptr= variable name*ptr= pointer(int, int)= points to a function taking twointintat the start = function returns
FAQ
What is a function pointer in C?
A function pointer is a variable that stores the address of a function, allowing you to call that function indirectly.
Why are function pointers used in C?
They are used for callbacks, custom behavior, dispatch tables, event handling, and standard library functions such as qsort.
How do I declare a function pointer in C?
Match the function's return type and parameters:
int (*ptr)(int, int);
Do I need to use & when assigning a function to a function pointer?
Usually no. ptr = add; works. ptr = &add; also works in common C usage.
How do I call a function through a pointer?
You can write either:
ptr(1, 2);
or:
(*ptr)(1, 2);
What happens if the function signature does not match?
That is incorrect and may produce compiler warnings or undefined behavior. The pointer type must match the function type.
Are function pointers the same as normal pointers?
Mini Project
Description
Build a small calculator program that uses a function pointer to choose an operation. This demonstrates the core value of function pointers: the same calling code can execute different functions depending on which function address is stored.
Goal
Create a calculator that can switch between addition, subtraction, and multiplication by assigning different functions to one function pointer.
Requirements
- Define at least three functions with the same signature: add, subtract, and multiply.
- Declare a function pointer that can point to any of those functions.
- Assign different functions to the pointer and call it multiple times.
- Print the operation results clearly.
- Keep the program in one C source file with a
mainfunction.
Keep learning
Related questions
Building More Fault-Tolerant Embedded C++ Applications for Radiation-Prone ARM Systems
Learn practical C++ and compile-time techniques to reduce soft-error damage in embedded ARM systems exposed to radiation.
Definition vs Declaration in C and C++: What’s the Difference?
Learn the difference between declarations and definitions in C and C++ with simple examples, common mistakes, and practical usage.
Difference Between #include <...> and #include "..." in C and C++
Learn the difference between #include with angle brackets and quotes in C and C++, including search paths, examples, and common mistakes.