Function Pointer in C (original) (raw)

Last Updated : 01 May, 2025

In C, a **function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves differently based on the context).

Let's take a look at an example:

C `

#include <stdio.h>

int add(int a, int b) { return a + b; }

int main() {

// Declare a function pointer that matches
  // the signature of add() fuction
int (*fptr)(int, int);

// Assign to add()
fptr = &add;

// Call the function via ptr
printf("%d", fptr(10, 5));
return 0;

}

`

**Explanation: In this program, we define a function **add(), assigns its address to a function pointer **fptr, and invokes the function through the pointer to print the sum of two integers.

**Function Pointer Declaration

Function pointers are declared according to the signature of the function they will be pointing to. Below is the generic syntax of function pointer declaration:

C `

return_type (*pointer_name)(parameter_types);

`

The parenthesis around the pointer_name is necessary, otherwise, it will be treated as a function declaration with the return type of return_type* and name pointer_name.

The type of the function is decided by its return type, number and type of parameters. So, the function **pointer should be declared in such a way that it matches the signature of the function it later points to. For example, in the above code, the function pointer was declared as:

C `

int (*fpr)(int, int);

`

which matches the signature of the **add() function that it later points to.

Initialization

A function pointer is then initialized by assigning the address of the function.

C `

pointer_name = &function_name

`

We can also skip the address of operator as function name itself behaves like a constant function pointer.

C `

pointer_name = function_name;

`

It is compulsory to assign the function with similar signature as specified in the pointer declaration. Otherwise, the compiler may show type mismatch error.

**Properties of Function Pointer

Function pointer points to the code instead of the data so there are some restrictions on the function pointers as compared to other pointers. Following are some important properties of function pointer:

**Applications with Examples

The following programs lists some common applications of function pointers along with code examples:

Function Pointer as Arguments (Callbacks)

One of the most useful applications of function pointers is passing functions as arguments to other functions. This allows you to specify which function to call at runtime.

C `

#include <stdio.h>

// A simple addition function int add(int a, int b) { return a + b; }

// A simple subtraction function int subtract(int a, int b) { return a - b; }

void calc(int a, int b, int (*op)(int, int)) { printf("%d\n", op(a, b)); }

int main() {

// Passing different 
// functions to 'calc'
calc(10, 5, add);
  calc(10, 5, subtract);
return 0;

}

`

**Explanation: The **calc function accepts a function pointer operation that is used to perform a specific operation (like addition or subtraction) on the two integers a and b. By passing the add or subtract function to calc, the correct function is executed dynamically.

Emulate Member Functions in Structure

We can create a data member inside **structure, but we cannot define a function inside it. But we can define function pointers which in turn can be used to call the assigned functions.

C `

#include <stdio.h>

// Define the Rectangle struct that contains pointers // to functions as member functions typedef struct Rect { int w, h; void (set)(struct Rect, int, int); int (area)(struct Rect); void (show)(struct Rect); } Rect;

// Function to find the area int area(Rect* r) { return r->w * r->h; }

// Function to print the dimensions void show(Rect* r) { printf("Rectangle's Width: %d, " "Height: %d\n", r->w, r->h); }

// Function to set width // and height (setter) void set(Rect* r, int w, int h) { r->w = w; r->h = h; }

// Initializer/constructor // for Rectangle void constructRect(Rect* r) { r->w = 0; r->h = 0; r->set = set; r->area = area; r->show = show; }

int main() { // Create a Rectangle object Rect r; constructRect(&r);

// Use r as a Rectangle
r.set(&r, 10, 5);
r.show(&r);
printf("Rectangle Area: %d", r.area(&r));
return 0;

}

`

Output

Rectangle's Width: 10, Height: 5 Rectangle Area: 50

Array of Function Pointers

You can also use function pointers in **arrays to implement a set of functions dynamically.

C `

#include <stdio.h>

// Function declarations int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int mul(int a, int b) { return a * b; } int divd(int a, int b) { return a / b; }

int main() {

// Declare an array of function pointers
int (*farr[])(int, int) = {add, sub, mul, divd};
int x = 10, y = 5;

// Dynamically call functions using the array
printf("Sum: %d\n", farr[0](x, y)); 
printf("Difference: %d\n", farr[1](x, y));
printf("Product: %d", farr[2](x, y));

return 0;

}

`

Output

Sum: 15 Difference: 5 Product: 50