Function Pointer in C

Function Pointer in C
Techiio-author
Written by Shuvhojit DebFebruary 3, 2022
10 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this blog, we'll know about the function pointer in C.

C Function Pointer

As we know that we can create a pointer of any data type such as int, char, float, we can also create a pointer pointing to a function. The code of a function always resides in memory, which means that the function has some address. We can get the address of memory by using the function pointer.

Syntax of Function Pointer

Syntax #1

Function_returntype ( *Pointer_name) ( argument_list)

First, we have to write the return type of function which can be void, double, etc. Then we have to give a name to our function pointer after that in another bracket we have to list the argument.

Syntax #2

void ( *funct_pointer ) ( int );

In the above syntax func_pointer is a pointer to a function taking an integer argument that will return void.

Working of Function Pointer in C

Let us have a look at the working of Function Pointer in the C programming language

Example #1

Here is the C code to demonstrate the working of Function Pointer:

Code:

#include <stdio.h>
int sum (int var1, int var2)
{
return var1 + var2 ;
}
int main ()
{
int ( *functiontopointer ) (int, int) ;
functiontopointer = sum ; // This part can also be done using void ( *fun_ptr ) ( int ) = &fun ;
//Here we are calling using the function pointer
int Output1 = functiontopointer (20, 98) ;
// Here we are calling function in normal way
int Output2 = sum (20, 98) ;
printf ( " Calling through function pointer: %d " , Output1 ) ;
printf ( " \n Calling through function name: %d " , Output2 ) ;
return 0 ;
}

Output:

Output 1 : Calling using the function pointer:  118
Output 2: Calling using the function name :  118 

Explanation: In the above code, you can see we have declared a method called Sum to add the two declared variables var1 and var2 and store the output of addition in Sum. In the main class, we have declared a function pointer FTP with an integer argument that returns void. Similarly, for using the function pointer functionality we are taking two inputs from the user. One is for showing the addition using function pointer while the other one is for showing the result using the normal pointer.

Example #2

Here is the C code to demonstrate the working of Function Pointer:

Code:

#include <stdio.h>
void addition ( int i , int j )
{
printf ( " The addition of both the input is %d \n " , i+j ) ;
}
void substraction ( int i , int j )
{
printf ( " The substraction of both the input is %d\n " , i-j ) ;
}
void multiplication ( int i , int j )
{
printf ( " The multiplication of both the input is %d\n " , i*j ) ;
}
int main()
{
void ( *funcpointer_arr[] )( int , int ) = { addition, substraction, multiplication} ;
int character, i = 8, j = 8 ;
printf ( " Please enter: 0 for Add , 1 for Substract and 2 "
"for Multiply \n " ) ;
scanf ( " %d ", &character ) ;
if ( character > 2 ) return 0 ;
( *funcpointer_arr [ character ] ) ( i ,  j ) ;
return 0 ;
}

Output:

blogpost

Explanation: In the above code, you can see we are applying the same technique of function pointer as we did in the previous code. We have separately created functions for addition, multiplication, and subtraction. We have declared a function pointer named void ( *funcpointer_arr [] )( int, int ) to store the values separately for all the operations with two integer data types a and b. Therefore, based on the choices 0, 1, and 2 we can perform the preferred operation separately.

Example #3

Here is another C code to demonstrate the working of Function Pointer:

Code:

#include <stdio.h>
#include <stdlib.h>
int comparison ( const void* lhs , const void* rhs )
{
return ( * ( int * ) rhs - * ( int * ) lhs ) ;
}
main()
{
int ( *cmp ) ( const void* , const void* ) ;
cmp = &comparison ;
int array [] = {15,42,93,54,15,66,37,8,81} ;
qsort ( array, sizeof ( array ) / sizeof ( *array ) , sizeof ( *array ) , cmp ) ;
int x = 0 ;
while ( x < sizeof ( array ) / sizeof ( *array ) )
{
printf ( " %d \t " , array [x] ) ;
x++ ;
}
}

Output:

blogpost

Explanation: In the above code, you can see we have declared a function pointer named as the comparison in which we have two arguments of type constant named as LHS and RHS, and the function is declared as integer, therefore, it will return this value ( * ( int * ) RHS – *( int * ) LHS ). Then in the main class, we are calling our function parameter and declaring a *camp ( pointer ) to store the address value of the comparison function pointer. We have declared an array of integer data types in which we have defined some random values in a random sequence. Then we are using a quick sort method to sort the array in decreasing order sequence in output. Finally, until the while condition is satisfied it will print the compared value on by one. Once the while condition turns false it will stop and print the final sorted result.

Conclusion

Function pointer in C programming language can make code faster, easy, short, and efficient without occupying any large space in the code as the function pointer contains the start of the executable code. We can also use a function name to get the address of a function pointer.

C Language
C Program
Function Pointer
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance