Back to C Programming
2026-07-155 min read

Function Pointers in C

Learn Function Pointers in C step by step with clear examples and exercises.

Why This Matters

Function pointers are a powerful feature of the C programming language that allows you to pass functions as arguments to other functions, or return them as values from other functions. In this article, we will explore why function pointers matter, their prerequisites, how they work, common mistakes, practice questions, and frequently asked questions.

Why This Matters

Function pointers are essential for understanding advanced C programming concepts such as callbacks, event-driven programming, and dynamic library loading. They are also crucial in system programming, device drivers, and operating systems. Function pointers can help you write more flexible, modular, and reusable code.

Prerequisites

Before diving into function pointers, it is essential to have a good understanding of the following concepts:

  • C Pointers: Understanding how pointers work in C is crucial for grasping function pointers. You should be comfortable with pointer arithmetic, dereferencing, and memory allocation.
  • Function Declarations: Knowledge of function declarations, including return types, parameter lists, and function prototypes, will help you understand how to declare and use function pointers.

Core Concept

A function pointer is a variable that stores the address of another function. To declare a function pointer, we specify its data type as the return type of the function it points to, followed by the name of the function it will replace. For example:

void (*func_ptr)(int); // Declaring a function pointer named func_ptr that can point to any function returning void and taking an int argument

To assign a function to a function pointer, we use the = operator, followed by the address of the function enclosed in parentheses:

func_ptr = &myFunction; // Assigning the address of myFunction to func_ptr

We can now call the function through the function pointer using the dereference operator *:

(*func_ptr)(5); // Calling myFunction with the argument 5 through the function pointer func_ptr

Function Pointers as Arguments and Return Values

We can pass a function pointer as an argument to another function, allowing for flexible and modular code. For example:

void myFunction(int num) {
printf("The number is %d\n", num);
}

void callMyFunction(void (*func)(int)) {
func(10); // Calling the function passed as an argument
}

int main() {
void (*ptr_to_myFunction)(int) = &myFunction; // Declaring and initializing a pointer to myFunction
callMyFunction(ptr_to_myFunction); // Passing ptr_to_myFunction as an argument to callMyFunction
return 0;
}

We can also have functions that return function pointers. For example:

void (*createFunc)(int) {
static void myFunc(int num) {
printf("The number is %d\n", num);
}
return &myFunc; // Returning the address of myFunc
}

int main() {
void (*ptr_to_created_func)(int) = createFunc(10); // Creating a function pointer that points to a new function created by createFunc
ptr_to_created_func(20); // Calling the newly created function through the function pointer
return 0;
}

Worked Example

Let's write a simple program that sorts an array using a user-defined comparison function. We will define a function compare that takes two integers and returns a negative, zero, or positive value based on the order of the numbers. We will then use this function as an argument to the qsort function from the standard library.

#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}

void printArray(int arr[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int arr[] = {5, 3, 1, 4, 2};
int size = sizeof(arr) / sizeof(arr[0]);

qsort(arr, size, sizeof(int), compare); // Sorting the array using our comparison function
printArray(arr, size); // Printing the sorted array

return 0;
}

Common Mistakes

  1. Forgetting to include the necessary header files (e.g., stdlib.h) for using functions like qsort.
  2. Failing to dereference the function pointer when calling the function through it (i.e., forgetting the *).
  3. Not specifying the correct return type and argument types in the function pointer declaration, resulting in compilation errors.
  4. Passing the wrong number or types of arguments to a function through a function pointer.
  5. Forgetting to return the address of a local variable when creating a function pointer within a function (e.g., in the createFunc example above).

Practice Questions

  1. Write a program that sorts an array using a user-defined comparison function that compares floating-point numbers.
  2. Modify the previous example to sort strings instead of integers, using a custom comparison function.
  3. Create a function sumArray that takes a pointer to an array and its size as arguments, and returns the sum of all elements in the array. Write a program that uses this function with a user-defined array.
  4. Implement a simple event loop using function pointers and callbacks. The event loop should call registered functions when specific events occur (e.g., timer expiration, keyboard input).

FAQ

  1. Why use function pointers instead of just calling the function directly? Function pointers provide flexibility by allowing you to pass functions as arguments or return them from other functions, enabling more modular and reusable code. They are also essential for some advanced programming concepts like event-driven programming and dynamic library loading.
  1. Can I assign a function pointer to another function pointer? Yes, but it is not common practice because it can lead to confusion about which function will be called. Instead, consider using a structure or an array of function pointers if you need multiple functions to be accessible from one variable.
  1. What happens when I try to modify the function through a function pointer? Attempting to modify a function through a function pointer results in undefined behavior because functions are not modifiable objects in C.
  1. Can I use function pointers with non-void return types? Yes, you can declare and use function pointers with any valid function signature, including those that return non-void values.
  1. What is the difference between a function pointer and a function reference? In C++, a function reference is similar to a function pointer but provides additional features like implicit type deduction and direct initialization. Function references are not available in standard C.