• 结构体数组中元素为函数


     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$

  • 相关阅读:
    HashMap循环遍历方式及其性能对比
    打印沙漏1
    第七周实验报告与总结5
    第四周总结与试验
    第六周实验报告4
    数据库学习之一
    Euler猜想
    pip安装模块
    python 自带的ide 不能保存文件
    javaWeb高级编程(1)
  • 原文地址:https://www.cnblogs.com/miry/p/5620923.html
Copyright © 2020-2023  润新知