GCC missing braces around initializer

2019-01-31 19:34发布

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};

8条回答
叛逆
2楼-- · 2019-01-31 20:12

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};
查看更多
Summer. ? 凉城
3楼-- · 2019-01-31 20:22

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)

查看更多
登录 后发表回答