首页 > 代码库 > 快捷底层函数结构体函数调用
快捷底层函数结构体函数调用
#include <stdio.h>
int Add(int x,int y);
float Sub(float x,float y);
float Mul(float x,float y);
float Exc(float x,float y);
int (*pMathAdd)(int x,int y);
float (*pMathSub)(float x,float y);
float (*pMathMul)(float x,float y);
float (*pMathExc)(float x,float y);
typedef struct __MATH
{
int (*pMathAdd)(int x,int y);
float (*pMathSub)(float x,float y);
float (*pMathMul)(float x,float y);
float (*pMathExc)(float x,float y);
}_MATH;
_MATH MATH = {
Add,Sub,Mul,Exc
};
float (*pMath[3])(float,float)={
Sub,Mul,Exc
};
void main(void)
{
printf("函数指针\n");
printf("%d\n",MATH.pMathAdd(9,3));
printf("%f\n",MATH.pMathSub(9,3));
printf("%f\n",MATH.pMathMul(9,3));
printf("%f\n",MATH.pMathExc(9,3));
printf("%f\n",(*pMath[0])(9,3));
printf("%f\n",(*pMath[1])(9,3));
printf("%f\n",(*pMath[2])(9,3));
while(1);
}
int Add(int x,int y)
{
return (x+y);
}
float Sub(float x,float y)
{
return (x-y);
}
float Mul(float x,float y)
{
return (x*y);
}
float Exc(float x,float y)
{
return (x/y);
}
快捷底层函数结构体函数调用