A function pointer in C is a pointer that stores the address of a function instead of the address of normal data. Just as an integer pointer points to an integer variable, a function pointer points to executable code with a specific signature. This makes function pointers one of the most powerful features in C because they allow a program to choose which function to call at runtime.
Function pointers are used in callbacks, menu systems, state machines, driver interfaces, interrupt tables, sorting logic, and many low-level designs where behavior must be selected dynamically. In this article, we will understand function pointer in C, its syntax, how declaration works, why parentheses matter, how to call a function through a pointer, and where this concept is used in real programs.
What is Function Pointer in C?
A function pointer in C is a variable that holds the address of a function. The function pointed to must match the expected return type and parameter list of the pointer declaration.
In simple words, a function pointer lets you treat a function like a value that can be stored, passed, and called later.
A function pointer does not store data. It stores the address of code that can be executed later.
Syntax of Function Pointer in C
The declaration syntax is the part that confuses most beginners. A common example is:
int (*funcPtr)(int, int);This means funcPtr is a pointer to a function that takes two int arguments and returns an int.
| Part | Meaning |
|---|---|
int | Return type of the function |
(*funcPtr) | funcPtr is a pointer |
(int, int) | The pointed function takes two integer arguments |
Why Parentheses are Important in Function Pointer Declaration
Parentheses are not decoration here. They change the meaning completely.
int (*funcPtr)(int, int); /* pointer to function */
int *funcPtr(int, int); /* function returning int pointer */In the first line, funcPtr is a pointer variable. In the second line, funcPtr is itself a function name. That is why function pointer syntax must be read carefully from the variable name outward.
Example of Function Pointer in C
The following example shows basic declaration, assignment, and function call through a function pointer.
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main(void)
{
int (*funcPtr)(int, int) = add;
int result = funcPtr(10, 20);
printf("%d\n", result);
return 0;
}Here, funcPtr stores the address of add, and calling funcPtr(10, 20) executes that function.
How Function Pointer Works in C
- A normal function is stored somewhere in program memory.
- The function name can act like the address of that function in many expressions.
- A function pointer variable stores that address.
- When the pointer is called, control jumps to the pointed function.
- The function executes using the provided arguments.
So a function pointer allows the program to delay the exact function choice until runtime.
Calling Function Through Pointer in C
Two common call styles are valid in C:
result = funcPtr(5, 3);
result = (*funcPtr)(5, 3);Both forms call the same function. The second form makes the pointer idea more explicit, while the first form is shorter and widely used.
Function Name and Function Pointer Relationship
In many expressions, the name of a function behaves like its address. That is why this assignment works:
int (*funcPtr)(int, int) = add;You may also see this written with the address-of operator:
int (*funcPtr)(int, int) = &add;Both are valid in practice. The function name already gives the function address in this context.
Passing Function Pointer as Argument in C
One of the most useful applications of function pointers is callbacks. A function can accept another function as an argument and call it when needed.
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int multiply(int a, int b)
{
return a * b;
}
int applyOperation(int x, int y, int (*op)(int, int))
{
return op(x, y);
}
int main(void)
{
printf("%d\n", applyOperation(4, 5, add));
printf("%d\n", applyOperation(4, 5, multiply));
return 0;
}This design is common when the same logic must work with different behaviors.
Array of Function Pointers in C
An array of function pointers can be used when a program must choose from multiple operations quickly.
#include <stdio.h>
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }
int main(void)
{
int (*ops[3])(int, int) = { add, sub, mul };
printf("%d\n", ops[0](8, 2));
printf("%d\n", ops[1](8, 2));
printf("%d\n", ops[2](8, 2));
return 0;
}This pattern is useful in calculators, command dispatch tables, and menu-driven systems.
Typedef with Function Pointer in C
Function pointer declarations can become hard to read. typedef makes them cleaner.
typedef int (*Operation)(int, int);
Operation op = add;Now Operation becomes a readable name for the function pointer type.
Function Pointer Inside Structure in C
Function pointers are often placed inside structures to build flexible interfaces.
#include <stdio.h>
typedef int (*Operation)(int, int);
typedef struct
{
const char *name;
Operation run;
} MathOp;
int add(int a, int b)
{
return a + b;
}
int main(void)
{
MathOp op = { "Addition", add };
printf("%d\n", op.run(7, 3));
return 0;
}This is a clean way to build driver tables, operation sets, and abstract interfaces in C.
Difference Between Function Pointer and Normal Pointer in C
| Point | Normal pointer | Function pointer |
|---|---|---|
| What it stores | Address of data | Address of a function |
| What it accesses | Variable value | Executable code |
| Common use | Data access and memory handling | Callbacks and dynamic behavior selection |
| Call syntax | Dereference to read or write data | Invoke to execute a function |
Function Pointer in Embedded C
Function pointers are very useful in embedded systems. They are often used in interrupt vector tables, state machines, bootloader logic, hardware abstraction layers, and driver interfaces.
- selecting driver behavior for different hardware
- mapping command IDs to handler functions
- building jump tables for fast dispatch
- implementing state-dependent behavior cleanly
- storing callbacks for events and interrupts
In embedded C, function pointers are powerful, but they must be used carefully because debugging indirect calls can be harder than debugging direct calls.
Common Mistakes with Function Pointer in C
- forgetting parentheses in the declaration
- using a function with the wrong parameter list or return type
- calling an uninitialized function pointer
- assuming a function pointer is the same as a normal data pointer
- making the syntax so complicated that the code becomes unreadable
- forgetting to check for
NULLwhen the pointer may be optional
| Mistake | Problem | Safer idea |
|---|---|---|
| Missing parentheses | Changes the declaration meaning | Read the declaration from the variable name outward |
| Incompatible function assignment | Undefined behavior or warnings | Match signature exactly |
| Calling without initialization | Crash or invalid jump | Initialize and check before use |
| Unreadable declarations | Harder maintenance | Use typedef for clarity |
Best Practices for Function Pointer in C
- Match the function signature exactly.
- Use
typedefwhen declarations become hard to read. - Check for
NULLbefore calling optional callbacks. - Use descriptive names for function pointer types and variables.
- Prefer simple dispatch tables over deeply nested conditional logic when appropriate.
FAQs
What is function pointer in C?
A function pointer in C is a pointer variable that stores the address of a function and can be used to call that function later.
How do you declare a function pointer in C?
A common example is int (*funcPtr)(int, int);, which declares a pointer to a function that takes two integers and returns an integer.
Why are parentheses needed in function pointer declaration?
The parentheses make it clear that the variable is a pointer. Without them, the declaration can mean something completely different.
Can a function pointer be passed to another function in C?
Yes. That is one of the main uses of function pointers and is the basis of callback design in C.
Can we create an array of function pointers in C?
Yes. Arrays of function pointers are useful when choosing one operation from multiple available functions.
Can a function pointer be NULL in C?
Yes. A function pointer can be set to NULL, and if that is possible in your design, it should be checked before calling it.