Skip to content

Function Pointers in C

A function pointer is a pointer that stores the address of a function instead of the address of a variable.

Just as a pointer to an int stores the address of an int variable, a function pointer stores the address of a function.

  • Pass functions as arguments to other functions (Callbacks)
  • Select a function at runtime
  • Implement menus and dispatch tables
  • Used extensively in operating systems, device drivers, and libraries like qsort()
return_type (*pointer_name)(parameter_list);

Example:

int (*pf)(int, int);

Meaning:

  • pf is a pointer.
  • It points to a function.
  • The function takes two int arguments.
  • The function returns an int.
int (*pf)(int, int);

Read inside-out:

(*pf) → pf is a pointer
(int, int) → points to a function taking two int arguments
int → function returns int
void (*p1)();

Pointer to a function returning void.

int (*p2)(int);

Pointer to a function taking one int and returning int.

float (*p3)(float, float);

Pointer to a function taking two floats and returning float.

char (*p4)(char);

Pointer to a function returning char.

#include<stdio.h>
int add(int a, int b)
{
return a + b;
}
int (*pf)(int, int);
pf = add;

or

pf = &add;

Both are correct because the function name automatically converts to its address.

Method

int result = pf(10, 20);

Method 2

int result = (*pf)(10, 20);

Both are equivalent.

#include<stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
int (*pf)(int, int);
pf = add;
printf("%d", pf(10,20));
return 0;
}

Output

30

Suppose

int add(int, int);

Then

add

means

Address of add()

Therefore,

pf = add;

is equivalent to

pf = &add;
Normal PointerFunction Pointer
Stores address of a variableStores address of a function
int *p;int (*pf)(int,int);
p = &x;pf = add;
*p gives valuepf() calls the function

1. Pointer to Function

int (*pf)(int, int);

pf is a pointer to a function.

2. Function Returning Pointer

int *pf(int, int);

pf is a function taking two int arguments and returning a pointer to int.

2. Pointer to Integer

int *p;

p points to an integer variable.

Function Returning Function?

int pf(int)(int); // Invalid. ❌

A function cannot return another function. It may return a pointer to a function.

int (*arr[3])(int, int);

Meaning:

  • arr is an array of 3 function pointers
  • Each pointer points to a function taking two ints and returning an int.

Example:

arr[0] = add;
arr[1] = sub;
arr[2] = mul;
printf("%d", arr[1](20,10));
void calculate(int a, int b,
int (*operation)(int,int))
{
printf("%d", operation(a,b));
}

Usage

calculate(10,20,add);

This is called a callback function.

int add(int,int);
int sub(int,int);
int (*select(int choice))(int,int)
{
if(choice==1)
return add;
else
return sub;
}

To read complicated declarations:

  1. Start from the variable name.
  2. Move right if possible.
  3. Move left.
  4. Resolve parentheses first.

Example

int (*pf)(int,int);
pf
(*pf) → pointer
(int,int) → to function
int → returning int
  • A function pointer stores the address of a function.

  • General syntax:

    return_type (*pointer_name)(parameters);
  • add and &add are equivalent when assigning to a function pointer.

  • Functions can be called using pf() or (*pf)().

  • A function cannot return another function, but it can return a pointer to a function.

  • Arrays of function pointers and function pointers as parameters are widely used for callbacks and dispatch tables.

  • Parentheses around *pointer_name are mandatory. Without them, the declaration has a completely different meaning.# Function Pointers in C