当前位置:   article > 正文

Linux GNU C结构体数组初始化示例_gcc结构体数组初始化

gcc结构体数组初始化

突然心血来潮,想学习一下结构体数组的初始化方面的知识。

GCC特有的数组初始化的风格:

  1. // 数组赋值另一种方式,但只在gcc下编译通过,g++不能
  2. enum {
  3. AAA = 0,
  4. BBB,
  5. CCC,
  6. DDD,
  7. };
  8. // 只对感兴趣的索引值进行赋值,而不管下标的顺序
  9. static const int regs[] = {
  10. [DDD] = 250,
  11. [CCC] = 3,
  12. [AAA] = 180,
  13. };


这种风格在kernel代码中经常看到。它不按照数组下标顺序,而是使用自定义的“索引”来赋值,比如Intel网卡igb驱动中一处代码:

 

 

  1. static const struct e1000_info *igb_info_tbl[] = {
  2. [board_82575] = &e1000_82575_info,
  3. };

从代码中可以清晰知道,这个结构体是针对82575的——board_82575比数字0更直观易读。
 

 

但是使用g++却无法编译,编译错误提示如下:

 

sorry, unimplemented: non-trivial designated initializers not supported

 

下面再看看结构体数组的初始化,先给出结构体的定义:

 

  1. enum control_type
  2. {
  3. TYPE_GPIO = 0,
  4. TYPE_IIC = 1,
  5. TYPE_SPI = 2,
  6. };
  7. typedef struct control_info_t {
  8. const char *name;
  9. enum control_type type;
  10. int enable;
  11. }control_info;


初始化方式一,依次给数组内元素赋值,如下::

 

 

  1. control_info control_info1[3] =
  2. {
  3. {"FOO_GPIO", TYPE_GPIO, 1},
  4. {"FOO_IIC", TYPE_IIC, 0},
  5. {"FOO_SPI", TYPE_SPI, 1},
  6. };

 

方式二,先声明再赋值,如下:

 

  1. control_info control_info[3];
  2. control_info[TYPE_SPI].name = "FOO_SPI";
  3. control_info[TYPE_SPI].type = TYPE_SPI;
  4. control_info[TYPE_SPI].enable = 1;

方式三,不按照顺序,只对感兴趣几项赋值,如下:

 

 

  1. control_info control_info2[3] =
  2. {
  3. [TYPE_SPI] = {"FOO_SPI", TYPE_SPI, 1},
  4. [TYPE_GPIO] = {"FOO_GPIO", TYPE_GPIO, 1},
  5. };

从这个方式上看,类型为SPI的“索引值”对应即为SPI,但无须理会类型SPI的具体值。

 

本文完整代码如下:

 

  1. /**
  2. 结果:
  3. struct test
  4. 250 0 180 3
  5. 111 name: FOO_IIC type: 1 enable: 0
  6. 222 name: FOO_GPIO type: 0 enable: 1
  7. 333 name: FOO_SPI type: 2 enable: 1
  8. */
  9. #include <stdio.h>
  10. #include <inttypes.h>
  11. ///
  12. // 简单int类型的数组
  13. // 数组赋值另一种方式,但只在gcc下编译通过,g++不能
  14. enum {
  15. AAA = 0,
  16. BBB,
  17. CCC,
  18. DDD,
  19. };
  20. // 只对感兴趣的索引值进行赋值,而不管下标的顺序
  21. static const int regs[] = {
  22. [DDD] = 250,
  23. [CCC] = 3,
  24. [AAA] = 180,
  25. };
  26. void struct_test1()
  27. {
  28. printf("%d %d %d %d\n", regs[DDD], regs[BBB], regs[AAA], regs[CCC]);
  29. }
  30. ///
  31. // 结构体数组初始化示例
  32. enum control_type
  33. {
  34. TYPE_GPIO = 0,
  35. TYPE_IIC = 1,
  36. TYPE_SPI = 2,
  37. };
  38. typedef struct control_info_t {
  39. const char *name;
  40. enum control_type type;
  41. int enable;
  42. }control_info;
  43. void struct_test_2()
  44. {
  45. // c++风格,用g++并使用 -std=c++11
  46. //control_info gcontrol_info0 {.name = "FOO", .type = TYPE_SPI, .enable=1};
  47. // 三个依次赋值
  48. control_info control_info1[3] =
  49. {
  50. {"FOO_GPIO", TYPE_GPIO, 1},
  51. {"FOO_IIC", TYPE_IIC, 0},
  52. {"FOO_SPI", TYPE_SPI, 1},
  53. };
  54. printf("111 name: %s type: %d enable: %d\n", control_info1[TYPE_IIC].name, control_info1[TYPE_IIC].type, control_info1[TYPE_IIC].enable);
  55. // GNU风格赋值
  56. control_info control_info2[3] =
  57. {
  58. [TYPE_SPI] = {"FOO_SPI", TYPE_SPI, 1},
  59. [TYPE_GPIO] = {"FOO_GPIO", TYPE_GPIO, 1},
  60. };
  61. printf("222 name: %s type: %d enable: %d\n", control_info2[TYPE_GPIO].name, control_info2[TYPE_GPIO].type, control_info2[TYPE_GPIO].enable);
  62. // 只有一个赋值
  63. control_info control_info[3];
  64. control_info[TYPE_SPI].name = "FOO_SPI";
  65. control_info[TYPE_SPI].type = TYPE_SPI;
  66. control_info[TYPE_SPI].enable = 1;
  67. printf("333 name: %s type: %d enable: %d\n", control_info[TYPE_SPI].name, control_info[TYPE_SPI].type, control_info[TYPE_SPI].enable);
  68. }
  69. int main(void)
  70. {
  71. printf("struct test\n");
  72. struct_test1();
  73. struct_test_2();
  74. return 0;
  75. }

 

李迟 2016.10.13 周四  中午午休前

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/518671
推荐阅读
相关标签
  

闽ICP备14008679号