How to initialize a struct in accordance with C programming language standards

前端 未结 15 2936
予麋鹿
予麋鹿 2020-11-21 22:59

I want to initialize a struct element, split in declaration and initialization. This is what I have:

typedef struct MY_TYPE {
  bool flag;
  short int value;         


        
15条回答
  •  暖寄归人
    2020-11-21 23:48

    I found another way to initialize structs.

    The struct:

    typedef struct test {
        int num;
        char* str;
    } test;
    

    Initialization:

    test tt = {
        num: 42,
        str: "nice"
    };
    

    As per GCC’s documentation, this syntax is obsolete since GCC 2.5.

提交回复
热议问题