Chinaunix首页 | 论坛 | 博客
  • 博客访问: 359127
  • 博文数量: 79
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 1370
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-12 08:48
个人简介

freedom~~~~~~~~~~

文章分类

全部博文(79)

文章存档

2014年(10)

2013年(2)

2012年(13)

2011年(54)

分类: C/C++

2011-09-24 10:21:20

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct a
  4. {
  5.     int (*func) (char *);
  6.     void (*fun) (int);
  7. };

  8. int hello(char *name)
  9. {
  10.     printf("------%s\n",name);
  11.     return 0;
  12. }

  13. void show(int data)
  14. {
  15.     printf("the give data is %d\n",data);
  16. }

  17. struct method
  18. {
  19.     void (*fun) (int);
  20. };

  21. void show1(int data)
  22. {
  23.     printf("this is show 1 %d\n",data*10);
  24. }
  25. void show2(int data)
  26. {
  27.     printf("this is show 2 %d\n",data*10);
  28. }
  29. void show3(int data)
  30. {
  31.     printf("this is show 3 %d\n",data*10);
  32. }

  33. struct method method_arry[]=
  34. {
  35.     show1,
  36.     show2,
  37.     show3,
  38. };

  39. int main()
  40. {
  41.     //在结构体定义中是函数,在下面中是成员,所以上面用;下面用,逗号
  42.     struct a a1=
  43.     {
  44.         hello,
  45.         show,
  46.     };

  47.     a1.func("imlink");
  48.     a1.fun(1111);
  49.     int i;
  50.     for(i=0;i<3;i++)
  51.     {
  52.         method_arry[i].fun(i);
  53.     }
  54.     //注意上面用method_arry[i].fun(i)调用,因为metod_arry中只有1个变量就是fun
  55.     //再数组初始化时只是对于不同到数组成员赋予了不同到值。
  56.     return 0;
  57. }
阅读(3906) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~