原来typedef void fun(void)和typedef void (*fun)(void)类似的,他们都是定义一函数类型,参数和返回值都是void.
用法示例:
#include <stdio.h>
int swap(int a ,int b)
{
if(a>b)
return a;
else
return b;
}
typedef int fun(int ,int); //定义一个已经存在的swap函数类型。
int t_swap(int a,int b, fun fun_swap ) //注意这里的 fun fun_swap
{
int res=fun_swap(a,b);
printf("%d",res);
return res;
}
main()
{
int temp;
temp=t_swap(5,6,swap); //注意这里直接填了swap
}
直接当参数的用法:
#include <stdio.h>
int swap(int a ,int b)
{
if(a>b)
return a;
else
return b;
}
int t_swap(int a,int b, int (*fun)(int ,int)) //直接把swap的指针传进去
{
int res=fun(a,b);
printf("%d",res);
return res;
}
main()
{
int temp;
temp=t_swap(5,6,swap); //注意这里直接填了swap
}