GCC missing braces around initializer

52,713

Solution 1

This is GCC bug # 53119:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119

If you want to see it fixed, post a followup to the bug report indicating that it's a problem for you.

Solution 2

Since your first member in the structure is an array you need:

static pkt_t stats = {{0}};

Outer braces are for the struct, inner braces are for the array. However, there are many other ways to skin this cat. (for instance, statics are already init'ed to zero)

Solution 3

If it is a global variable or a local static one, it's automatically initialized. So, simply:

static pkt_t stats;

Solution 4

One way is to initialize every member of the struct inside the braces, rather than relying on the implicit zero filling. For array members, you need another {} which is likely causing the warning. Another is to just disable the warning, though this isn't recommended as it can also catch legitimate bugs.

Solution 5

If you still have the joy being on a gcc version which omits this false warning, with a struct like this in the question you can avoid this problem with some simple restructuring like this:

typedef struct {
    uint32_t timeouts;
    uint32_t crc_errors;
    uint32_t incoming[FRAME_TYPE_MAX];
    uint32_t outgoing[FRAME_TYPE_MAX];
} pkt_t;

static pkt_t stats = {0};
Share:
52,713
fred basset
Author by

fred basset

Engineer working with C, C++, assembler and embedded hardware.

Updated on July 17, 2022

Comments

  • fred basset
    fred basset almost 2 years

    I have this struct in C below that I want to initialize to all zero. How do I get rid of the missing braces warning?

    typedef struct {
        uint32_t incoming[FRAME_TYPE_MAX];
        uint32_t outgoing[FRAME_TYPE_MAX];
        uint32_t timeouts;
        uint32_t crc_errors;
    } pkt_t;
    
    static pkt_t stats = {0};