Chinaunix首页 | 论坛 | 博客
  • 博客访问: 515052
  • 博文数量: 56
  • 博客积分: 1136
  • 博客等级: 少尉
  • 技术积分: 1378
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-28 14:02
文章存档

2014年(1)

2013年(7)

2012年(45)

2011年(3)

分类: 嵌入式

2012-04-06 16:16:10

在底层待的时间太长,竟还没接触这种回调函数的思想,有一次面试时碰到了,惭愧啊

点击(此处)折叠或打开

  1. /* para_callback.h */
  2. #ifndef PARA_CALLBACK_H
  3. #define PARA_CALLBACK_H

  4. typedef void (*callback_t)(void *);
  5. extern void repeat_three_times(callback_t, void *);

  6. #endif

  7. void repeat_three_times(callback_t f, void *para)
  8. {
  9.      f(para);
  10.      f(para);
  11.      f(para);
  12. }
  13. /* main.c */
  14. #include <stdio.h>

  15. void say_hello(void *str)
  16. {
  17.      printf("Hello %s\n", (const char *)str);
  18. }

  19. void count_numbers(void *num)
  20. {
  21.      int i;
  22.      for(i=1; i<=(int)num; i++)
  23.      printf("%d ", i);
  24.      putchar('\n');
  25. }

  26. int main(void)
  27. {
  28.      repeat_three_times(say_hello, "Guys");
  29.      repeat_three_times(count_numbers, (void *)4);
  30.      return 0;
  31. }

阅读(3009) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~