MISRA C:2012 9.3 clarification

Moderators: misra-c, david ward

Post Reply
mcks
Posts: 1
Joined: Mon Nov 23, 2020 8:54 am
Company: Microchip

MISRA C:2012 9.3 clarification

Post by mcks » Tue Nov 24, 2020 9:19 am

I try to implement the checks for "9.3 - Arrays shall not be partially initialized" in Cppcheck, and would like to have some clarifications.

Is it ok to mix initializers like this, as long as every element gets initialized?

int32_t a[3] = { 1, 2, [2]=3 };

For multi-dimensional arrays, this would be compliant:

int32_t b[2][2] = { { 1, 2 }, { [1]=4 } };

beacuse b[1] is initialized with only a designated initializer.


As for c, both the array subobjects c[0] and c[1] are fully initialized, but there is no initialization of c[2]. Does that make the statement non-compliant?

int32_t c[3][2] = { { 1, 2 }, { 3, 4 } };


What about the arrays below, are they compliant or not, and why? Also, are they violatin 9.2 or 9.4?
int32_t d[1][2] = { [0][1]=2, [0]={1} };
int32_t e[1][2] = { [0]={1}, [0][1]=2 };
int32_t f[3][2] = { [1]={3, 4}, { 5, 6 }, [0][1]=2, [0][0]=1 };
int32_t g[2][2] = { { 1, 2 }, [1][0]=3, 4 };

Let me know what you think. If you have more (tricky) examples, then please share!

Post Reply

Return to “8.9 Initialization”