zoukankan      html  css  js  c++  java
  • 结构体数组中元素为函数

     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	%c
    ",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	%c
    ",a1[i].a,a1[i].b);
    29     }
    30 }

    结果也能正常显示

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

  • 相关阅读:
    Redis介绍
    getch
    gecher
    C语言中的sleep函数
    sleep
    C语言中的System()函数
    System的使用
    函数参数的传递方式
    C语言strlen()函数:返回字符串的长度
    strlen
  • 原文地址:https://www.cnblogs.com/miry/p/5620923.html
Copyright © 2011-2022 走看看