我在Linux中遇到过这个代码构造,想要理解它
struct mystruct {
int x;
int b[40];
};
/*later */
static struct mystruct e = { .x = 5,
.b = {-1},
};
.b = {-1}是做什么的?它是否只初始化数组b的第一个或所有元素?它是如何工作的?
答案 0 :(得分:3)
static struct mystruct e = {
.x = 5,
.b = {-1},
};
这里将b [0]初始化为-1。其他元素初始化为0.
答案 1 :(得分:1)
这意味着初始化结构的成员b,其数组以-1开头,后跟0
.b =
是GCC扩展。 (如评论所述,从C99开始,这也是标准的一部分){-1}
是标准数组初始化。