Designated initializers and compound literals for struct in C

前端 未结 2 764
别跟我提以往
别跟我提以往 2020-12-11 19:03

I have following struct:

typedef struct my_struct {
    int a;
    int b;
    int *c;
} my_struct;

is:

my_struct n = (my_st         


        
2条回答
  •  忘掉有多难
    2020-12-11 19:36

    Is my_struct n = (my_struct) { .b = 3 }; equivalent to my_struct n = (my_struct) { .a = 0, .b = 3, .c = NULL };?

    Yes. A compound literal may fail to provide full initialization, in which case any unimitialized members initialize to zero (NULL is case of pointer member) by default.

    What about my_struct n = (my_struct) { .b = 3, 0 };?

    Member b and c will be initialized to 3 and 0 respectively while a will be initialized to 0 by default.

提交回复
热议问题