Yes, it is possible to declare a pointer that points to a function (i.e. function pointer) in C.
Syntax for function pointer declaration :
Syntax for function pointer declaration :
<return type of function> (*<ptr_name>) (arguments)
Example :
#include<stdio.h>
#include<conio.h>
int add(int,int); // fun_prototypevoid main()
{
int ans;int (*p)(int,int); // p is a pointer to a function which take 2 integers arguments and return an integer.
p=add; // p now points to add function
ans=(*p)(10,20); // calling of add() function using function pointer
printf(“answer is %d”,ans);
}
int add(int a,int b)
{
return (a+b);
}
Output :
answer is 30