C語言部分初始化一個結構體

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章