Can I create a pointer that points to a function in C ?

Yes, it is possible to declare a pointer that points to a function (i.e. function pointer) in C.

Syntax for function pointer declaration :

 

<return type of function> (*<ptr_name>) (arguments)

Example :

#include<stdio.h>
#include<conio.h>
int add(int,int); // fun_prototype

void 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

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.