Course Content
C Tutorial
About Lesson

Function Pointers

Function pointers in C are powerful and versatile tools that allow you to create more flexible and efficient code. They essentially point to functions, enabling dynamic invocation and manipulation of functions within a program. Here’s a comprehensive overview of function pointers in C.

What Are Function Pointers?

In C, functions are treated as first-class citizens, meaning they can be passed as arguments to other functions and returned as values. Function pointers, therefore, hold the memory address of a function. They enable you to call functions indirectly through the pointer variable.

Declaring Function Pointers

The syntax for declaring a function pointer involves specifying the return type and parameters of the function it points to. For example:

int (*funcPtr)(int, int);

This declares funcPtr as a pointer to a function that takes two int parameters and returns an int.

Assigning Function Pointers

Assigning a function to a function pointer involves referencing the function name without parentheses. For instance:

int sum(int a, int b) {
return a + b;

int (*funcPtr)(int, int);
funcPtr = sum;

Here, funcPtr points to the sum function.

Using Function Pointers

Once assigned, function pointers can be used to invoke the function they point to. This is done by dereferencing the pointer and providing arguments, similar to a regular function call:

int result = (*funcPtr)(3, 5);

This line invokes the sum function indirectly through funcPtr with arguments 3 and 5, storing the result in the result variable.

Practical Use Cases

Function pointers find applications in various scenarios, such as implementing callback mechanisms, creating generic functions, and facilitating dynamic function invocation.

Callback Functions

Callback functions are functions passed as arguments to other functions to be called within the receiving function. Function pointers enable the implementation of callback mechanisms, allowing for flexibility in program behavior.

Generic Functions

Function pointers can be used to create generic functions that can operate on different types of data. By utilizing function pointers, you can write reusable code that adapts to varying requirements.

Dynamic Function Invocation

In situations where the exact function to be called is determined at runtime, function pointers provide a means to invoke functions dynamically based on program conditions