回调函数: 既然是回调,就肯定会是在某一个地方。将这个回调函数赋值调用

 1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <unistd.h>
  4 
  5 //定义函数指针
  6 typedef int (listener_t) (int listener, int arg);
  7 
  8 //回调
  9 static int test(listener_t *enumerator, int inerst, int arg)
 10 {   
 11     (*enumerator) (inerst,arg);
 12     printf("inster test fuction.\n");
 13     
 14     return 0;
 15 }
 16 
 17 //测试函数
 18 static int func(int insert, int arg)
 19 {   
 20     printf("old:%d  next:%d\n",insert,arg);
 21     
 22     return 0;
 23 }
 24 
 25 int main(void)
 26 {   
 27     int i = 0,num;
 28     
 29     
 30     for(i = 0; i < 10; i++){
 31         printf("this is %d\n time is %d\n",i+1,i+1);
 32         sleep(1);
 33         num = test(func,i,i+1); //赋值
 34         if(num != 0){
 35             printf("Function pointer return failed."\n");
 36         }
 37 
 38     }
 39     return 0;
 40 }