Function Pointers in C

Introduction

C provides us with the concept of Pointers which are variables that can store the address of other variables. It means that a variable that has some valid space in the memory and stores some data can be accessed using its address in the memory.

C also supports the facility of a pointer to a function. In this article, we will learn about the Function Pointers in C. But, before that, you have to be well-versed in the concept of variable pointers. So, we will see how pointers work in C.  The following code shows the implementation of a pointer variable.

#include<stdio.h>
int main(){
    int a=10;
    int* pointer= &a;//declaration of pointer variable
    //this prints the address
    printf("The address of variable a :%d\n", pointer);
    //this prints the value stored i.e. 10
    printf("The data stored in variable a : %d\n", *pointer);

}

Output:

The address of variable a :287158748
The data stored in variable a : 10

write your code here: Coding Playground

Here, the variable ‘pointer’ stores the address of variable ‘a’ and can be used to access the value.  You may get confused with “int* pointer” and “int *pointer.” Both are the same, but it is good practice to use an asterisk with the data type of the pointer variable. The reason is that “int*” means a pointer of type ‘int.’ So, an asterisk (*) is not part of the variable, it’s a part of the data- type of the pointer declared.

You can also change the value of a variable without using it using its pointer variable. The following code shows how to change the value of a variable without using it.

#include<stdio.h>
int main(){
    int a=10;
    int* pointer= &a;//declaration of pointer variable
    printf("Value of a: %d\n", a);
    printf("Address of a: %d\n", &a);
    //changing value of a
    *pointer=11;//value changed using pointer
    printf("Value of variable a  %d\n", a);
    printf("address of variable a  %d\n", &a);
}

Output:

Value of a: 10
Address of a: -982376132
Value of variable a  11
address of variable a  -982376132

write your code here: Coding Playground

Hence, the value of the variable has been changed from 10 to 11 without using it. This is the utility of pointers. Now, it will be simpler for us to understand the concept of Function Pointers in C.

When you declare a function, it is allocated some space in the stack memory. Thus, each function has a valid address. For example, in the code below, the address of the main function and display function is printed.

#include<stdio.h>
void display();
int main(){
printf("address if main function : %d\n", &main); 
printf("address if display function : %d\n", &display);


}
void display(){
    printf("hello world");
}

Output:

address if main function : 2113933641
address if display function : 2113933716

write your code here: Coding Playground

Thus, Function Pointers in C can be used to store the address of a function that is defined in the program. Now, let’s see how to declare and define pointers to function in C.

The syntax of Function Pointers is as follows:

return_type (*variable)(arguementType_1, arguementType_2,....)

Suppose, there is a  function that adds two numbers.  Its address can be stored in the following way.

#include<stdio.h>
int Sum();
int main(){
    int  (*ptrToFunc)(int,int)=&Sum;//declaration of pointer variable
    int result1, result2;
    result1= (*ptrToFunc)(18, 18); // calling using (ptrToFunc)(10,18)also   
                                      works
    result2= Sum(10, 18);


    printf("Sum using Pointer to function : %d\n", result1);
    printf(" Sum using normal function calling : %d\n", result2);
}
int Sum(int a, int b){
    return a+b;
}

Here, ptrToFunc is the pointer variable that returns ‘int’ and accepts two arguments of type ‘int.’ The output of the code is:

Sum using Pointer to function : 36
Sum using normal function calling : 28

The main point is that a function pointer is a pointer to a block of code, unlike the normal pointers which point to some data. Therefore, a function pointer signifies the starting of a block of code. Also, we cannot allocate and deallocate memory using the pointer to function in C.

write your code here: Coding Playground

Passing address of a function using Function Pointers in C

You can also pass the address of a function as an argument to another function using pointers. In the code given below, there are two functions, function1 and function2. The pointer to function1 and function2 is called with a pointer to function as an argument. Then, the passed pointer is used to call the function1 and after function1 is executed, control is passed to function2 (from where it was called) and finally to the main function.

#include<stdio.h>
//using function pointers to pass functions as argument
void function1(int a);
void function2(void (*ptrToFunc1)(int a ));
int main(){
    void (*ptrToFunc1)(int a)= &function1 ; // pointer to function1
    function2(ptrToFunc1 ); //passing function's address as argument
}
void function1(int a){
    printf("Function 1 called \n");
    a*=a;
    printf("%d\n", a);
}
void function2(void(*ptrToFunc1)( int a)){//function's address is passed into function
    printf("Function 2 called \n");
    (*ptrToFunc1)(19); //calling function using pointer 
 
}

The output of the above code is given below:

Function 2 called
Function 1 called
361

write your code here: Coding Playground

The Array of Pointers to Functions in C

Like the array of pointers, we can also have an array of Function Pointers in C. The syntax to declare an array of pointers to functions is:

return_type (*pointerName[])(arg1, arg2, arg3, .....)

The elements of an array of pointers to functions are the same as that of an array, where the array elements are accessed using the index of elements. Below is the C implementation of an array of Function Pointers in C.

#include<stdio.h>
void number();
void square();
void cube();
int main(){
    //array of pointers to function in C
    //void is the return type of all functions
    //arrOfPtrFunc[] denotes array of pointers to functions which take argument of type 'int'
 


    void (*arrOfPtrToFunc[])(int )={number, square, cube};
    //calling the functions
    printf("Functions are being called using pointers to functions\n");
    int size= sizeof(arrOfPtrToFunc)/sizeof(arrOfPtrToFunc[0]);//determining size of array
    printf("Enter any number\n");
    int number;
    scanf("%d", &number);
    for(int i=0;i<size;i++){
        arrOfPtrToFunc[i](number);
    }
}
void number(int a){
    printf("Function1: The number is %d\n", a);
}
void square(int a){
    printf("Function2: Square of number is %d\n", a*a);
}


void cube(int a){
    printf("Function3: Cube of number is %d", a*a*a);
}

The output of the above code is:

Functions are being called using pointers to functions
Enter any number
6
Function1: The number is 6
Function2: Square of number is 36
Function3: Cube of number is 216

As shown in the output, the functions have been called using the array of function pointers in C. Therefore, using pointers you cannot only access the variables, but also the functions which are defined in the program.

write your code here: Coding Playground