结构体数组中元素为函数
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$