C Function Pointers
In this page:
What is a Function Pointer?
A function pointer stores the memory address of a function instead of a data value, letting you call that function indirectly through the pointer rather than by its name directly. This is the foundation for writing code that can decide which function to run at runtime instead of hardcoding the choice.
Example: What is a Function Pointer?
#include <stdio.h>
int square(int n) {
return n * n;
}
int main() {
int (*funcPtr)(int) = square;
printf("%d", funcPtr(5));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Syntax of Function Pointers
Declaring a function pointer requires wrapping the pointer name and its asterisk in parentheses, like int (*compare)(int, int);, because without the parentheses the compiler would instead parse it as a function that returns a pointer. Getting this syntax right is the single most common stumbling block when first working with function pointers.
Example: Syntax of Function Pointers
#include <stdio.h>
int compare(int a, int b) {
return a - b;
}
int main() {
int (*compareFn)(int, int) = compare;
printf("%d", compareFn(5, 3));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Passing Function Pointers to Functions
Passing a function pointer as an argument lets a general-purpose function call back into caller-supplied logic without knowing what that logic actually does, which is exactly how the standard library's qsort() lets you sort any data using a comparison function you provide. This keeps the general function reusable across many different use cases.
Example: Passing Function Pointers to Functions
#include <stdio.h>
int square(int n) { return n * n; }
void apply(int (*fn)(int), int value) {
printf("%d", fn(value));
}
int main() {
apply(square, 4);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Array of Function Pointers
Storing function pointers in an array creates a jump table, where selecting an index picks which function runs — a common pattern for implementing menus, state machines, or opcode dispatch without a long chain of if/else statements. Each array element is just a function address, so calling table[choice]() invokes the right handler directly.
Example: Array of Function Pointers
#include <stdio.h>
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int main() {
int (*ops[2])(int, int) = {add, sub};
printf("%d", ops[0](3, 4));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Common Mistakes with Function Pointers
A function pointer's declared signature (its return type and parameter types) must exactly match the function it's assigned to point at; assigning a mismatched signature compiles in C but produces undefined behavior when called. Always double-check that a callback's declared type matches the function you're actually registering.
Example: Common Mistakes with Function Pointers
#include <stdio.h>
int square(int n) { return n * n; }
int main() {
int (*correctPtr)(int) = square;
printf("%d", correctPtr(5));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions