Function Pointers in C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

6/16/24, 1:04 PM Function Pointers in C

Function Pointers in C

What is Function Pointer 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.

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.

Declaring a Function Pointer


You should have a function whose function pointer you are going to declare. To
declare a function pointer in C, write a declarative statement with the return type,
pointer name, and parameter types of the function it points to.

Declaration Syntax

The following is the syntax to declare a function pointer:

function_return_type(*Pointer_name)(function argument list)

Example

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)();".

https://www.tutorialspoint.com/cprogramming/c_function_pointers.htm 1/6
6/16/24, 1:04 PM Function Pointers in C

Function Pointer Example


The following example demonstrates how you can declare and use a function pointer
to call a function.

#include <stdio.h>

// Defining a function
void hello() { printf("Hello World"); }

// The main code


int main() {
// Declaring a function pointer
void (*ptr)() = &hello;

// Calling function using the


// function poiter
(*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


A function pointer can also be declared for the function having arguments. During
the declaration of the function, you need to provide the specific data types as the
parameters list.

https://www.tutorialspoint.com/cprogramming/c_function_pointers.htm 2/6
6/16/24, 1:04 PM Function Pointers in C

Understanding 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 of Function Pointer with Arguments

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

https://www.tutorialspoint.com/cprogramming/c_function_pointers.htm 3/6
6/16/24, 1:04 PM Function Pointers in C

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 −

Understanding Pointer to Function with Pointer Arguments

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 of Pointer to Function with Pointer Arguments

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;

https://www.tutorialspoint.com/cprogramming/c_function_pointers.htm 4/6
6/16/24, 1:04 PM Function Pointers in 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 of Array of Function Pointers

We can use the property of dynamically calling the function through the pointers
instead of if-else 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;
}

https://www.tutorialspoint.com/cprogramming/c_function_pointers.htm 5/6
6/16/24, 1:04 PM Function Pointers in C

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.

https://www.tutorialspoint.com/cprogramming/c_function_pointers.htm 6/6

You might also like