Introduction to Function Pointer in C
A Function pointer is the most important feature in C which is also known as Subroutine pointer. A pointer that points to any function is called a Function Pointer. It points to a specific part of code when executing difference is that a function pointer to code as compare to a normal point which points to a specific variable in code. The most valuable thing is that we can pass the function parameter as an argument and its name can be used to get the address of the function. FP is like a variable storing the address of the function.
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 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:
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:
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:
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.
Recommended Articles
This is a guide to Function Pointer in C. Here we discuss two syntaxes, the working of Function Pointer in C programming language with examples. You can also go through our other related articles to learn more –