Pointer to function
It holds the base address of function definition in memory.
Declaration
datatype (*pointername) ();
The name of the function itself specifies the base address of the function. So, initialization is done using function name.
For example,
int (*p) (); p = display; //display () is a function that is defined.
Example 1
We shall see a program for calling a function using pointer to function −
#include<stdio.h>
main (){
int (*p) (); //declaring pointer to function
clrscr ();
p = display;
*(p) (); //calling pointer to function
getch ();
}
display (){ //called function present at pointer location
printf(“Hello”);
}Output
Hello
Example 2
Let us consider another program explaining the concept of pointer to function −
#include <stdio.h>
void show(int* p){
(*p)++; // add 1 to *p
}
int main(){
int* ptr, a = 20;
ptr = &a;
show(ptr);
printf("%d", *ptr); // 21
return 0;
}Output
21