C Function Pointers
In this page:
return_type (*pointer_name)(parameter_types) = function_name;
pointer_name(arguments);
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 standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
// Send n * n back to the caller
return n * n;
}
// Program execution starts in main()
int main() {
int (*funcPtr)(int) = square;
// Print formatted text to the screen
printf("%d", funcPtr(5));
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
int compare(int a, int b) {
// Send a - b back to the caller
return a - b;
}
// Program execution starts in main()
int main() {
int (*compareFn)(int, int) = compare;
// Print formatted text to the screen
printf("%d", compareFn(5, 3));
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) { return n * n; }
void apply(int (*fn)(int), int value) {
// Print formatted text to the screen
printf("%d", fn(value));
}
// Program execution starts in main()
int main() {
apply(square, 4);
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
// Program execution starts in main()
int main() {
int (*ops[2])(int, int) = {add, sub};
// Print formatted text to the screen
printf("%d", ops[0](3, 4));
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) { return n * n; }
// Program execution starts in main()
int main() {
int (*correctPtr)(int) = square;
// Print formatted text to the screen
printf("%d", correctPtr(5));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Writing
int *f(int)when you mean a function pointer, which declares a function returning a pointer; it must beint (*f)(int). - Calling through a
NULLfunction pointer, which crashes. - Assigning a function with a different signature to the pointer, which gives undefined behavior when called.
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