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

前端 未结 15 2689
予麋鹿
予麋鹿 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:39

    In (ANSI) C99, you can use a designated initializer to initialize a structure:

    MY_TYPE a = { .flag = true, .value = 123, .stuff = 0.456 };
    

    Edit: Other members are initialized as zero: "Omitted field members are implicitly initialized the same as objects that have static storage duration." (https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html)

    0 讨论(0)
  • 2020-11-21 23:40

    a = (MYTYPE){ true, 15, 0.123 };

    would do fine in C99

    0 讨论(0)
  • 2020-11-21 23:43

    C programming language standard ISO/IEC 9899:1999 (commonly known as C99) allows one to use a designated initializer to initialize members of a structure or union as follows:

    MY_TYPE a = { .stuff = 0.456, .flag = true, .value = 123 };
    

    It is defined in paragraph 7, section 6.7.8 Initialization of ISO/IEC 9899:1999 standard as:

    If a designator has the form
    . identifier
    then the current object (defined below) shall have structure or union type and the identifier shall be the name of a member of that type.

    Note that paragraph 9 of the same section states that:

    Except where explicitly stated otherwise, for the purposes of this subclause unnamed members of objects of structure and union type do not participate in initialization. Unnamed members of structure objects have indeterminate value even after initialization.

    In GNU GCC implementation however omitted members are initialized as zero or zero-like type-appropriate value. As stated in section 6.27 Designated Initializers of GNU GCC documentation:

    Omitted field members are implicitly initialized the same as objects that have static storage duration.

    Microsoft Visual C++ compiler should support designated initializers since version 2013 according to official blog post C++ Conformance Roadmap. Paragraph Initializing unions and structs of Initializers article at MSDN Visual Studio documentation suggests that unnamed members initialized to zero-like appropriate values similarly to GNU GCC.

    ISO/IEC 9899:2011 standard (commonly known as C11) which had superseded ISO/IEC 9899:1999 retains designated initializers under section 6.7.9 Initialization. It also retains paragraph 9 unchanged.

    New ISO/IEC 9899:2018 standard (commonly known as C18) which had superseded ISO/IEC 9899:2011 retains designated initializers under section 6.7.9 Initialization. It also retains paragraph 9 unchanged.

    0 讨论(0)
  • 2020-11-21 23:45

    I've been looking for a nice way to initialize my struct, and I've got to using the below (C99). This lets me initialize either a single structure or an array of structures in the same way as plain types.

    typedef struct {
        char *str;
        size_t len;
        jsmntok_t *tok;
        int tsz;
    } jsmn_ts;
    
    #define jsmn_ts_default (jsmn_ts){NULL, 0, NULL, 0}
    

    This can be used in the code as:

    jsmn_ts mydata = jsmn_ts_default; /* initialization of a single struct */
    
    jsmn_ts myarray[10] = {jsmn_ts_default, jsmn_ts_default}; /* initialization of
                                                        first 2 structs in the array */
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 23:48

    I didn't like any of these answers so I made my own. I don't know if this is ANSI C or not, it's just GCC 4.2.1 in it's default mode. I never can remember the bracketing so I start with a subset of my data and do battle with compiler error messages until it shuts up. Readability is my first priority.

        // in a header:
        typedef unsigned char uchar;
    
        struct fields {
          uchar num;
          uchar lbl[35];
        };
    
        // in an actual c file (I have 2 in this case)
        struct fields labels[] = {
          {0,"Package"},
          {1,"Version"},
          {2,"Apport"},
          {3,"Architecture"},
          {4,"Bugs"},
          {5,"Description-md5"},
          {6,"Essential"},
          {7,"Filename"},
          {8,"Ghc-Package"},
          {9,"Gstreamer-Version"},
          {10,"Homepage"},
          {11,"Installed-Size"},
          {12,"MD5sum"},
          {13,"Maintainer"},
          {14,"Modaliases"},
          {15,"Multi-Arch"},
          {16,"Npp-Description"},
          {17,"Npp-File"},
          {18,"Npp-Name"},
          {19,"Origin"}
        };
    

    The data may start life as a tab-delimited file that you search-replace to massage into something else. Yes, this is Debian stuff. So one outside pair of {} (indicating the array), then another pair for each struct inside. With commas between. Putting things in a header isn't strictly necessary, but I've got about 50 items in my struct so I want them in a separate file, both to keep the mess out of my code and so it's easier to replace.

    0 讨论(0)
提交回复
热议问题