C Program to Create function pointer
Write a C Program to Create Function Pointer ?
In this ongoing C programming tutorial series, we learned many C programming language.In this tutorial, we will learn how to declare a function pointer and what is a function pointer.
A pointer is the backbone of c language, without the pointers, we cannot imagine the C Language. A good cognizance of pointers enables the programmer to indite the optimize and robust code.
In the C language, a pointer is like a variable but only there is one distinction between the pointers and variables. A variable store value but pointer store address of the variable or other pointers.
C programming code to create function pointer:
#include<stdio.h>
int add(int a,int b);
int sub(int a,int b);
int mul(int a,int b);
int div(int a,int b);
int mode(int a,int b);
struct foo
{
//int a;
int (*ope9r)(int b,int c);
};
int main()
{
int ans,i,j,a,b;
struct foo foo1[5];
printf(“enter where u want add”);
scanf(“%d”,&i);
foo1[i].ope9r=add;
printf(“enter where u want sub”);
scanf(“%d”,&i);
foo1[i].ope9r=sub;
printf(“enter where u want mul”);
scanf(“%d”,&i);
foo1[i].ope9r=mul;
printf(“enter where u want div”);
scanf(“%d”,&i);
foo1[i].ope9r=div;
printf(“enter where u want mod”);
scanf(“%d”,&i);
foo1[i].ope9r=mode;
printf(“enter the value of a,b”);
scanf(“%d%d”,&a,&b);
printf(“enter the key what u want to perform”);
scanf(“%d”,&j);
//for(j=0;j<5;j++)
ans=foo1[j].ope9r(a,b);
printf(“%d\n”,ans);
return 0;
}
int add(int a,int b)
{
return(a+b);
}
int sub(int a,int b)
{
return(a-b);
}
int mul(int a,int b)
{
return(a*b);
}
int div(int a,int b)
{
return(a/b);
}
int mode(int a,int b)
{
return(a%b);
}
Leave a comment if you have any problem,we will try our best to solve your problem.