[英]How to initialize array in an array of structs?
Let's assume I have a structure defined as: 假设我的结构定义为:
typedef struct _TStruct {
uint Values[3];
} TStruct;
Then I define an array of structures: 然后定义一个结构数组:
TStruct Data[3];
How do I correctly initialize the arrays in this array of structures? 如何正确初始化此结构数组中的数组?
To correctly initialize an array in an array of structures you need to do the following: 要在结构数组中正确初始化数组,您需要执行以下操作:
typedef struct _TStruct {
uint Values[3];
} TStruct;
TStruct Data[3] = {
{{ 0x86, 0x55, 0x79 }}, {{ 0xaa, 0xbb, 0xcc }}, {{ 0x76, 0x23, 0x24 }}
};
Pay attention to the double braces around every group of values. 注意每组值周围的双括号。 The additional pair of braces is essential to avoid getting a following gcc error (only when
-Wall
flag is present, precisely it's "detected" by gcc
-Wmissing-braces
flag): 另外一对大括号对于避免出现后续gcc错误至关重要(仅当存在
-Wall
标志时,才由gcc
-Wmissing-braces
标志“检测到”):
warning: missing braces around initializer
警告:初始化器周围缺少花括号
Note : 注意事项 :
Usage of double braces {{ }}
does not change the layout of data in memory 使用双括号
{{ }}
不会更改内存中数据的布局
This warning does not appear on MS Visual Studio C++ compiler MS Visual Studio C ++编译器上未出现此警告
See also: 也可以看看:
How to repair warning: missing braces around initializer? 如何修复警告:初始化程序周围缺少花括号?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.