← Back to C Course | Chapter 12: Advanced Topics | Lesson 6 of 20

C Function Pointers

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?

c
#include <stdio.h>
int square(int n) {
	return n * n;
}
int main() {
	int (*funcPtr)(int) = square;
	printf("%d", funcPtr(5));
	return 0;
}

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

c
#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;
}

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

c
#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;
}

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

c
#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;
}

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

c
#include <stdio.h>
int square(int n) { return n * n; }
int main() {
	int (*correctPtr)(int) = square;
	printf("%d", correctPtr(5));
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.