最近看http协议,有许多的回调函数,让我想起了以前做快速排序void qsort()的时候编写了快排序的第四个参数让我写了很久但是最后依然没有理解,现在对回调函数做一个总结,现在从无参回调函数开始理解。
首先头文件:
#include <stdio.h>
然后给函数指针写个好听的名字:callBackFun
typedef void (* callBackFun)(void);
接着写第一个回调函数
void aPrint()
{
printf("hello");
return ;
}
继续写第二个回调函数
void bPrint()
{
printf(" 白鱼儿!!!\n");
return ;
}
最后写一个参数带有函数指针的执行函数就大工告成了
void func(callBackFun p)
{
p();
return ;
}
//测试程序如下
#include <stdio.h>
typedef void (* callBackFun)(void);
void aPrint()
{
printf("hello");
return ;
}
void bPrint()
{
printf(" 白鱼儿!!!\n");
return ;
}
void func(callBackFun p)
{
p();
return ;
}
int main()
{
func(aPrint);
func(bPrint);
return 0;
}
打印结果:
hello 白鱼儿!!!