Chinaunix首页 | 论坛 | 博客
  • 博客访问: 59101
  • 博文数量: 19
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 20
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-12 11:10
个人简介

Stupid is as stupid does

文章分类

全部博文(19)

文章存档

2021年(1)

2017年(1)

2016年(17)

我的朋友

分类: C/C++

2016-06-08 11:00:25

  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. }
阅读(779) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~