码迷,mamicode.com
首页 > 编程语言 > 详细

结构体数组中元素为函数

时间:2016-06-27 19:49:42      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include<stdio.h>
 2 typedef struct A
 3 {
 4   int a;
 5   char b;
 6 } a;
 7 
 8 int foo()
 9 {
10   return 1;
11 }
12 
13 char boo()
14 {
15   return b;
16 }
17 int main(int args,char * arg[])
18 {
19   int i;
20   a a1[]=
21   {
22     {foo(),boo()},
23     {foo(),boo()},
24     {foo(),boo()}
25 };
26 for(i=0;i<sizeof(a1)/sizeof(a);i++)
27 {
28   printf("%d\t%c\n",a1[i].a,a1[i].b);
29 }
30 }

 

这里能正常输出

-bash-3.2$ ./a.out 
1 b
1 b
1 b
-bash-3.2$

 

若把结构体的数据类型改变:

 1 #include<stdio.h>
 2 typedef struct A
 3 {
 4     int a;
 5     int b;
 6 } a;
 7 
 8  int foo()
 9  {
10      return 1;
11  }
12 
13 char boo()
14 {
15     return b;
16 }
17 int main(int args,char * arg[])
18 {
19     int i;
20     a a1[]=
21     {
22         {foo(),boo()},
23         {foo(),boo()},
24         {foo(),boo()}
25     };
26     for(i=0;i<sizeof(a1)/sizeof(a);i++)
27     {
28         printf("%d\t%c\n",a1[i].a,a1[i].b);
29     }
30 }

结果也能正常显示

-bash-3.2$ ./a.out
1 b
1 b
1 b
-bash-3.2$

 

结构体数组中元素为函数

标签:

原文地址:http://www.cnblogs.com/miry/p/5620923.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!