-
- 如果参数是一个函数指针,调用者可以传递一个函数的地址给实现者,让实现者去调用它,这称为回调函数(Callback Function)。例如qsort(3)和bsearch(3)。
- 表 24.7. 回调函数示例:void func(void (*f)(void *), void *p);
- 调用者实现者
-
- 1. 提供一个回调函数,再提供一个准备传给回调函数的参数。
-
- 2. 把回调函数传给参数f,把准备传给回调函数的参数按void *类型传给参数p
-
-
-
- 1. 在适当的时候根据调用者传来的函数指针f调用回调函数,将调用者传来的参数p转交给回调函数,即调用f(p);
-
-
-
- 以下是一个简单的例子。实现了一个repeat_three_times函数,可以把调用者传来的任何回调函数连续执行三次。
- 例 24.7. 回调函数
- /* para_callback.h */
- #ifndef PARA_CALLBACK_H
- #define PARA_CALLBACK_H
-
- typedef void (*callback_t)(void *);
- extern void repeat_three_times(callback_t, void *);
-
- #endif
- /* para_callback.c */
- #include "para_callback.h"
-
- void repeat_three_times(callback_t f, void *para)
- {
- f(para);
- f(para);
- f(para);
- }
- /* main.c */
- #include <stdio.h>
- #include "para_callback.h"
-
- void say_hello(void *str)
- {
- printf("Hello %s\n", (const char *)str);
- }
-
- void count_numbers(void *num)
- {
- int i;
- for(i=1; i<=(int)num; i++)
- printf("%d ", i);
- putchar('\n');
- }
-
- int main(void)
- {
- repeat_three_times(say_hello, "Guys");
- repeat_three_times(count_numbers, (void *)4);
- return 0;
- }
|