MISRA Discussion Forums

Full Version: Rule 9.2 zero initialization of array of struct
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I Have the following code:
#include <stdint.h>

typedef struct {
    int16_t foo;
    int16_t bar;
} foobar_t;

static foobar_t foobars[42] = {0};

In rule 9.2 the following is given:

Quote:Note also that all the elements of arrays or structures can be initialised (to zero or NULL) by giving an
explicit initialiser for the first element only. If this method of initialisation is chosen then the first
element should be initialised to zero (or NULL), and nested braces need not be used.

Is the zero initialization of foobars compliant with Rule 9.2?

Lint is complaining about this kind of initialization, but I'm not sure if it is right or not
Warning: LINT [W940] omitted braces within an initializer [MISRA 2004 Rule 9.2]

The rule mentions that the first element should be initialized to zero. The first element, which can be listed in the initialization list, would be the member foo of the first struct of the first array element. Setting this to 0 would, in my understanding, comply with rule 9.2. As in the last sentence "... nested braces need not be used" is mentioned, no further braces around the 0 are required.
The zero initialization of foobars is compliant with MISRA-C:2004 Rule 9.2.