回调指针回调函数的使用(C语言)

来源:互联网 发布:java求最大公约数 编辑:程序博客网 时间:2024/05/21 13:55

回调函数的使用

回调函数在C语言中是通过函数指针来实现的,通过将回调函数的地址传给被调函数从而实现回调。因此,要实现回调,必须首先定义函数指针。

1. 回调指针

概念:指针是一个变量,是用来指向内存地址的。一个程序运行时,所有和运行相关的物件都是需要加载到内存中,这就决定了程序运行时的任何物件都可以用指针来指向它。函数是存放在内存代码区域内的,它们同样有地址,因此同样可以用指针来存取函数,把这种指向函数入口地址的指针称为函数指针。

1. 采用函数调用的一般形式

首先看一个hello world!的程序:

int application_start( void ){     OSStatus err = kNoErr;     char *s ="hello world !";     app_log(" s:%s",s);     return err;}

打印的结果是:

[0][TCP: main.c:  90]  s:hello world !

如果采用函数调用的形式来实现:

//声明void Islog( char *s);int application_start( void ){     OSStatus err = kNoErr;     Islog("hello world !");     return err;}void Islog( char *s){     app_log(" s:%s",s);}

打印的结果是:

[0][TCP: main.c:  90]  s:hello world !

2. 简单的函数指针的应用

形式1:返回类型(*函数名)(参数表)

把上面的例子改成使用简单的函数指针的写法:

//声明void Islog( char *s);int application_start( void ){     OSStatus err = kNoErr;     void (*fp)(char *s);//声明一个函数指针(fp)     fp = Islog;//将Islog的函数入口地址付给fp     fp("hello world !");//函数指针fp实现函数调用     return err;}void Islog( char *s){     app_log(" s:%s",s);}

打印的结果是:

[0][TCP: main.c:  90]  s:hello world !

由上知道:函数指针函数的声明之间唯一区别就是:用指针名(*fp)代替了函数名Islog,这样声明了一个函数指针,然后进行赋值fp= Islog 就可以进行指针函数的调用了,声明函数指针时,只要返回值类型、参数个数、参数类型等保持一致,就可以声明一个函数指针了。注意,函数指针必须括号括起来 void (*fp)(char *s)。

3. 使用typedef更简单

实际中,为了方便,通常使用宏定义的方式声明函数指针。

形式2:返回类型(*新类型)(参数表)

typedef void (*intFunc)(int);

此宏定义的意思是要定义的类型是void (*)(int),即参数一个int,什么也不返回的函数指针,定义的别名是intFunc。

采用宏定义的方式声明函数指针将上上面的代码改写:

//宏定义typedef void(*FP)(char *s);//声明void Islog( char *s);int application_start( void ){     OSStatus err = kNoErr;     FP fp; //通常使用宏FP来声明一个函数指针fp     fp = Islog;//将Islog的函数入口地址付给fp     fp("hello world !");//函数指针fp实现函数调用     return err;}void Islog( char *s){     app_log(" s:%s",s);}

打印的结果是:

[0][TCP: main.c:  90]  s:hello world !

4. 函数指针数组

下面的是指针函数数组的例子:

例子1:

//宏定义typedef void(*FP)(char *s);//声明void Islog1( char *s);void Islog2( char *s);void Islog3( char *s);int application_start( void ){     OSStatus err = kNoErr;  //void* Islog[] = {Islog1,Islog2,Islog3};//定义了指针数组,这里a是一个普通指针  //Islog3[0] ("hello world !");//编译错误,指针数组不能用下标的方式来调用函数     FP Islog[] = {Islog1,Islog2,Islog3};//定义一个函数指针的数组,这里的f是一个函数指针     Islog[0] ("hello world!");     Islog[1] ("hello world!");     Islog[2] ("hello world!");     return err;}void Islog1( char *s){app_log(" s:%s",s);}void Islog2( char *s){app_log(" s:%s",s);}void Islog3( char *s){app_log(" s:%s",s);}

打印的结果是:

[0][TCP: main.c: 101]  s:hello world![4][TCP: main.c: 103]  s:hello world![7][TCP: main.c: 105]  s:hello world!

例子2:

//宏定义typedef void(*FP)( char *s,int count);//声明void Islog1( char *s,int count);void Islog2( char *s,int count);void Islog3( char *s,int count);int application_start( void ){     OSStatus err = kNoErr;  //void* Islog[] = {Islog1,Islog2,Islog3};//定义了指针数组,这里a是一个普通指针  //Islog3[0] ("hello world !");//编译错误,指针数组不能用下标的方式来调用函数     FP Islog[] = {Islog1,Islog2,Islog3};//定义一个函数指针的数组,这里的f是一个函数指针     Islog[0] ("hello world!",1);     Islog[1] ("hello world!",2);     Islog[2] ("hello world!",3);     return err;}void Islog1( char *s,int count){app_log(" s:%s and count:%d",s,count);}void Islog2( char *s,int count){app_log(" s:%s and count:%d",s,count);}void Islog3( char *s,int count){app_log(" s:%s and count:%d",s,count);}

打印的结果是:

[0][TCP: main.c: 101]  s:hello world! and count:1[5][TCP: main.c: 102]  s:hello world! and count:2[9][TCP: main.c: 103]  s:hello world! and count:3

2. 回调函数

概念:回调函数,顾名思义,就是使用者自己定义一个函数,使用者自己实现这个函数的程序内容,然后把这个函数作为参数传入别人(或系统)的函数中,由别人(或系统)的函数在运行时来调用的函数。函数是你实现的,但由别人(或系统)的函数在运行时通过参数传递的方式调用,这就是所谓的回调函数。简单来说,就是由别人的函数运行期间来回调你实现的函数。再来看看来自Stack Overflow某位大神简洁明了的表述:A “callback” is any function that is called by another function which takes the first function as a parameter。 也就是说,函数 F1 调用函数 F2 的时候,函数 F1 通过参数给 函数 F2 传递了另外一个函数 F3 的指针,在函数 F2 执行的过程中,函数F2 调用了函数 F3,这个动作就叫做回调(Callback),而先被当做指针传入、后面又被回调的函数 F3 就是回调函数。到此应该明白回调函数的定义了吧?

我们将一开始的hello world函数修改成函数回调样式:

1. 简单的回调函数

//定义回调函数void PrintText(){     app_log("hello world!");}//定义实现回调函数的“调用函数”void CallprintfText(void (*callfunc)()){     callfunc();}int application_start( void ){     OSStatus err = kNoErr;     CallprintfText(PrintText);     return err;}

打印的结果是:

[0][TCP: main.c:  82] hello world!

2. 使用typedef写法:

typedef void (*CallFunc)();//定义回调函数void PrintText(){     app_log("hello world!");}//定义实现回调函数的“调用函数”void CallprintfText(CallFunc callfunc){     callfunc();}int application_start( void ){     OSStatus err = kNoErr;     CallprintfText(PrintText);     return err;}   

打印的结果是:

[0][TCP: main.c:  82] hello world!

3. 修改带参数的回调函数写法

这里只放了一个类型的参数,很重要!

void PrintText(char *s){     app_log("s:%s",s);}//定义实现回调函数的“调用函数”void CallprintfText(void (*callfunc)(char*),char *s){    callfunc(s);}int application_start( void ){     OSStatus err = kNoErr;     CallprintfText(PrintText,"hello world!");     return err;}

打印的结果是:

[0][TCP: main.c:  82] hello world!

4. 使用typedef写法:

参数类型两种

typedef void (*CallFunc)(char *s,int count);//定义回调函数void PrintText(char *s,int count){     app_log("s:%s",s);     app_log("count:%d",count);}//定义实现回调函数的“调用函数”void CallprintfText(CallFunc callfunc,char *s,int count){     callfunc(s,count);}int application_start( void ){     OSStatus err = kNoErr;     CallprintfText(PrintText,"hello world!",1);     return err;}

打印的结果是:

[0][TCP: main.c:  84] s:hello world![3][TCP: main.c:  85] count:1