Function Pointer example program in C programming
A function pointer is a pointer variable that can store address of a function and then using the function pointer we can call initialized function in our program.
Function Pointer Example using C program
Declaration of function pointer
return_type (*fun_pointer_name)(argument_type_list);
Initialization of function pointer
fun_pointer_name= &function_name;
fun_pointer_name= function_name;
Calling of function pointer
*(fun_pointer_name)(actual_argument_list);
Declaration with Initialization
return_type (*fun_pointer_name)(argument_type_list)= &function_name;
Consider the example
Output:
int addTwoNumbers(int x,int y);
This function will take two integer arguments and returns addition of those numbers.
int (*ptr_sum)(int,int);
This is the declaration of the function pointer for addTwoNumbers function.
ptr_sum=&addTwoNumbers;
This statement is initializing the function pointer with address of function addTwoNumbers.
(*ptr_sum)(a,b);
This statement is the calling the function ptr_sum and ptr_sum will point to the addTwoNumbers.
need an explanation for this answer? contact us directly to get an explanation for this answer