【发布时间】:2021-08-02 23:33:51
【问题描述】:
在复习我的 C 知识时,我偶然发现了以下示例:
#include <stdio.h>
/* Just a simple structure */
typedef struct lab {
int num;
char *str;
} bal;
int main (void) {
/* Declare and _partially_ initialize an array of the structure above... */
bal struct_array[10] = { {0, NULL} };
/* Question: what does _exacly_ happen to the other 9 members of the array? */
return 0;
};
代码中的注释应该足以提供我的问题。换句话说,如果我们部分初始化一个结构数组会发生什么?当然,我知道(至少)对于 C++11 有默认初始化。但它也适用于纯 C 吗?如果是,是所有标准(从 C89 开始)都是如此,还是仅适用于某些标准?谢谢。
【问题讨论】:
-
我认为你甚至可以通过
bal struct_array[10] = {{0}};获得相同的结果 -
是的,既然我们知道要保留的默认初始化,我当然同意 '={{0}}';和其他提议的变体。
-
@mediocrevegetable1
= {0};也可以用作“通用初始化器”。
标签: c struct initialization