Struct initialization of the C/C++ programming language?

前端 未结 5 1532
无人及你
无人及你 2020-11-27 21:46

I could do struct initialization with code:

struct struct_type_id struct_name_id = { value1, value2, value3 };

but could not with:

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 22:26

    I don't know why C didn't originally support some kind of syntax to 'reinitialize' a struct using something like the initializer list - there are definitely times when I would have found it handy. As Juliano mentioned, C99 (and C++0x) has fixed it to a certain degree, but I often have to stick with C90. When I want to do something like that, I'll sometimes do the following:

    struct foo const init_foo = { 1, 2, 3};
    struct foo myFoo;
    
    // ....
    
    myFoo = init_foo;  // reinitialize myFoo
    

提交回复
热议问题