I've a question regarding the following code:
#include "all_needed.h"
static uint8_t array[2] = {0};
void main(void)
{
...
}
Is a (module) global array allowed to be initialized as above for having each member set to zero while being ANSI C conform?
I've got a problem in Code Composer 5 (MSP430 Project) where I had to change it into
static uint8_t array[2] = {0, 0};
for a correct initialization of the 2nd member of the array.
Yes, this is allowed, and should initialize the array to zero. C99, §6.7.8 p10:
If an object that has static storage duration is not initialized explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules;
— if it is a union, the first named member is initialized (recursively) according to these rules.
and p21:
If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
Report the bug to your compiler vendor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With