void process()函数和result(*fun)()干嘛的?
#include <stdio.h>void main()
{
int max(int, int); /*函数声明*/
int min(int, int); /*函数声明*/
int add(int, int); /*函数声明*/
void process( int x, int y, int(*fun)() ); /*函数声明 */
int a, b;
printf("Endter a and b: ");
scanf("%d %d", &a, &b);
printf("max = ");
process(a, b, max);
printf("min = ");
process(a, b, min);
printf("sum = ");
process(a, b, add);
}
int max(int x, int y)
{
int z;
if(x > y)
{
z = x;
}
else
{
z = y;
}
return z;
}
int min(int x, int y)
{
int z;
if(x < y)
{
z = x;
}
else
{
z = y;
}
return z;
}
int add(int x, int y)
{
int z;
z = x + y;
return z;
}
void process( int x, int y, int(*fun)() )
{
int result;
result = (*fun)(x,y);
printf("%d\n", result);
}
void process()函数和result(*fun)()干嘛的?
函数指针呗~
(*fun)() 和 x ,y 一样 是process函数的参数之一
类型都是int型是一个地址(函数指针)
process函数每次都把接收到的函数地址(max min add)
去匹配相对应的函数就相当于接口
然后再通过process调用相应的函数得到相应的结果 26process.cpp(20) : error C2664: 'process' : cannot convert parameter 3 from 'int (int,int)' to 'int (__cdecl *)(void)'
None of the functions with this name in scope match the target type
E:\编程\C例子\26process.cpp(22) : error C2065: 'min' : undeclared identifier
E:\编程\C例子\26process.cpp(24) : error C2664: 'process' : cannot convert parameter 3 from 'int (int,int)' to 'int (__cdecl *)(void)'
None of the functions with this name in scope match the target type
E:\编程\C例子\26process.cpp(41) : error C2373: 'min' : redefinition; different type modifiers
E:\编程\C例子\26process.cpp(62) : error C2197: 'int (__cdecl *)(void)' : too many actual parameters
为什么编译会出现这个情况呢!
页:
[1]