https://stackoverflow.com/questions/10828294/c-and-c-partial-initialization-of-automatic-structure
In C, objects are never partially initialised - if any part of them is initialised, the entire object (and all sub-objects recursively) are initialised. If no explicit initialiser is provided then elements are initialised to "zero of the appropriate type".
related:
CFLAGS += -Wno-missing-field-initializers
example:
#include <stdio.h>
struct position {
int x;
int y;
int z;
};
int main() {
struct position point = {1,2};
printf("x=%d, y=%d, z=%d\n", point.x, point.y, point.z);
}
./a.out
you will get:
x=1, y=2, z=0
本文探讨了C语言中结构体的初始化过程,特别是在部分初始化时的行为。通过一个具体示例,展示了当仅初始化结构体的部分成员时,未被显式初始化的成员将被默认设置为零。此外,文章还提到了编译器警告选项-Wno-missing-field-initializers,用于忽略字段未初始化的警告。
310

被折叠的 条评论
为什么被折叠?



