Skip to main content
Function pointer
C allows operations with pointer to function.Every function code along with its variables is allocated some space in the memory.thus,every function has an address.Function pointer are pointer variables that point the address of a function.Function pointer can be declared,assigned values,and used to access the functions they point to.This is a useful technique for passing a function as an argument to another function.In order to declare a pointer to a function we have to declare it like the prototype of the function except that name of the function is enclosed between parentheses () and an asterisk (*) is inserted between name.
Initializing a function pointerAs in case of other pointer variables,a function pointer must be initialized prior to use.If we have declared a pointer to the function,then that pointer can be assigned the address of the correct function,just by using its name.In case of an array, a function name is changed into an address when it's used in an expression.It is optional to use address operator (&) in front the function name.
Write a program to calling a function using a function pointer
#include<stdio.h>
void print(int n);
void (*fp) (int);
main()
{
fp=print;
(*fp)(10);
fp(20);
return 0;
}
void print(int value)
{
printf("\n %d",value);
}
Output
1020
Comments
Post a Comment