C程序不知道怎么理解

来源:百度知道 编辑:UC知道 时间:2024/06/13 15:00:51
这是单片机c51书上的,书中有很多错误的(问人的),
不知道下面怎么理解或是有没有错误?
void init_uart(void);
int max(int x,int y);
int min(int x,int y);
void process(int x,int y,int (code *fun)(int x,int y) );
void main()
{
int (code *pf)(int x,int y);
int a=2,b=6;
init_uart();
printf("a=%d,b=%d\n",a,b);
printf("max=");
pf=max;
process(a,b,pf);
printf("min=");
pf=min;
process(a,b,pf);
while(1)
{}
}
int max(int x,int y)
{
return x>=y?x:y;
}
int min(int x,int y)
{
return x<=y?x:y;
}
void process(int x,int y,int (code *fun)(int x,int y) ) //这行后面“(code *fun)(int x,int y)”中的 x和y是从哪传来的?
{
int result;
result=(*fun)(x,y);
printf("%d\n",result);
}
void init_uart(void)
{
//.........
}

(code *fun)(int x,int y)

这是一个函数指针

作为process的第三个参数

具体可以参考标准库中的qsort的最后一个参数的用法

对!int (code *fun)(int x,int y)其实是定义了一个函数指针,后面的result=(*fun)(x,y)才是对此函数的调用。而此函数的申明是在main函数的第一句话,后面肯定有int (code *pf)(int x,int y)这个函数的实现,不知道对你有没有帮助