自定义类型的 C 数组中独立方括号的含义是什么?
typedef enum {
BAR_1 = 0,
BAR_2,
BAR_3,
} bar_types;
typedef struct {
int is_interesting;
int age;
} foo_s;
static foo_s bars[] = {
[BAR_1] = {1, 2}, /* What is being done here? */
[BAR_2] = {1, 41},
[BAR_3] = {0, 33},
};
在上面的代码中, 的含义是[BAR_1] = {1, 2}
什么?什么时候可以使用独立的方括号?
我注意到,如果我在括号中添加重复值,clang 会发出有关子对象初始化的警告。
static foo_s bars[] = {
[BAR_1] = {1, 2},
[BAR_2] = {1, 41},
[BAR_3] = {0, 33},
[BAR_3] = {0, 33},
};
-----
$clang example.c
example.c:17:19: warning: subobject initialization
overrides initialization of other fields within its
enclosing subobject [-Winitializer-overrides]
[BAR_3] = {0, 33},
^~~~~~~
什么是 C 子对象?