MISRA Discussion Forums
Rule 9.2 zero initialization of array of struct - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17)
+---- Forum: 6.9 Initialisation (https://forum.misra.org.uk/forumdisplay.php?fid=36)
+---- Thread: Rule 9.2 zero initialization of array of struct (/showthread.php?tid=1617)



Rule 9.2 zero initialization of array of struct - sowisojh - 27-04-2022

I Have the following code:
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.


RE: Rule 9.2 zero initialization of array of struct - misra-c - 22-05-2022

The zero initialization of foobars is compliant with MISRA-C:2004 Rule 9.2.