default value for struct member in C

后端 未结 16 2260
-上瘾入骨i
-上瘾入骨i 2020-11-27 10:53

Is it possible to set default values for some struct member? I tried the following but, it\'d cause syntax error:

typedef struct
{
  int flag = 3;
} MyStruct         


        
16条回答
  •  伪装坚强ぢ
    2020-11-27 11:51

    An initialization function to a struct is a good way to grant it default values:

    Mystruct s;
    Mystruct_init(&s);
    

    Or even shorter:

    Mystruct s = Mystruct_init();  // this time init returns a struct
    

提交回复
热议问题