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

C Function Pointers

A function pointer holds the address of a function, like a note saying where a recipe is. You can then call the function through it or pass it around.
Syntax
c
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?

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

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

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

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

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 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing int *f(int) when you mean a function pointer, which declares a function returning a pointer; it must be int (*f)(int).
  2. Calling through a NULL function pointer, which crashes.
  3. Assigning a function with a different signature to the pointer, which gives undefined behavior when called.

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.