• C Programming Video Tutorials

Function Pointers in C



A pointer in C is a variable that stores the address of another variable. Similarly, a variable that stores the address of a function is called a function pointer or a pointer to a function. Function pointers can be useful when you want to call a function dynamically. The mechanism of callback functions in C is dependent on the function pointers.

Syntax: Pointer to a Function

Function Pointers point to code like normal pointers. In Functions Pointers, the function’s name can be used to get function’s address. A function can also be passed as an argument and can be returned from a function.

function_return_type(*Pointer_name)(function argument list)

Here is a simple hello() function in C −

void hello(){
   printf("Hello World");
}

We declare a pointer to this function as follows −

void (*ptr)() = &hello;

We can now call the function with the help of this function pointer "(*ptr)();".

Example

The following example demonstrates how it works −

#include <stdio.h>

void hello(){

   printf("Hello World");
}

int main(){
   
   void (*ptr)() = &hello;
   
   (*ptr)();
   
   return 0;
}

Output

When you run this code, it will produce the following output −

Hello World

Note: Unlike normal pointers which are data pointers, a function pointer points to code. We can use the name of the function as its address (as in case of an array). Hence, the pointer to the function hello() can also be declared as follows −

void (*ptr)() = hello;

Function Pointer with Arguments

Suppose we have a function called addition() with two arguments −

int addition (int a, int b){

   return a + b;
}

To declare a function pointer for the above function, we use two arguments −

int (*ptr)(int, int) = addition;

We can then call the function through its pointer, by passing the required arguments −

int z = (*ptr)(x, y);

Try the complete code as below −

Example

Here is the complete code. It shows how you can call a function through its pointer −

#include <stdio.h>

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

int main(){

   int (*ptr)(int, int) = addition;
   int x = 10, y = 20;
   int z = (*ptr)(x, y);

   printf("Addition of x: %d and y: %d = %d", x, y, z);
   
   return 0;
}

Output

When you run this code, it will produce the following output −

Addition of x: 10 and y: 20 = 30

Pointer to Function with Pointer Arguments

We can also declare a function pointer when the host function itself as pointer arguments. Let us look at this example −

We have a swap() function that interchanges the values of "x" and "y" with the help of their pointers −

void swap(int *a, int *b){
   int c;
   c = *a;
   *a = *b;
   *b = c;
}

By following the syntax of declaring a function pointer, it can be declared as follows −

void (*ptr)(int *, int *) = swap;

To swap the values of "x" and "y", pass their pointers to the above function pointer −

(*ptr)(&x, &y);

Example

Here is the full code of this example −

#include <stdio.h>

void swap(int *a, int *b){
   int c;
   c = *a;
   *a = *b;
   *b = c;
}

int main(){
   
   void (*ptr)(int *, int *) = swap;
   
   int x = 10, y = 20;
   printf("Values of x: %d and y: %d before swap\n", x, y);
   
   (*ptr)(&x, &y);
   printf("Values of x: %d and y: %d after swap", x, y);
   
   return 0;
}

Output

Values of x: 10 and y: 20 before swap
Values of x: 20 and y: 10 after swap

Array of Function Pointers

You can also declare an array of function pointers as per the following syntax:

type (*ptr[])(args) = {fun1, fun2, ...};

Example

We can use the property of dynamically calling the function through the pointers instead of if-then or switch-case statements. Take a look at the following example −

#include <stdio.h>

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

float subtract(int a, int b){
   return a - b;
}

float multiply(int a, int b){
   return a * b;
}

float divide(int a, int b){
   return a / b;
}

int main(){

   float (*ptr[])(int, int) = {add, subtract, multiply, divide};
   
   int a = 15, b = 10;
   
   // 1 for addition, 2 for subtraction 
   // 3 for multiplication, 4 for division
   int op = 3;
   
   if (op > 5) return 0;
   printf("Result: %.2f", (*ptr[op-1])(a, b));
   
   return 0;
}

Output

Run the code and check its output −

Result: 150.00

Change the value of op variable to 1, 2 or 4 to get the results of other functions.

Advertisements