Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does "static/extern uint8_t array[2] = {0};" conform to the ANSI C specification?

Tags:

c

embedded

msp430

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.

like image 895
Tild Avatar asked Nov 15 '11 15:11

Tild


1 Answers

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.

like image 157
Stephen Canon Avatar answered Oct 12 '22 21:10

Stephen Canon