4

What is the meaning of this initialization:

char arr[10] = { 0, };

I'm familiar with char arr[10] = {0}; which sets all the elements to zero, and with char arr[10] = {1,2}; which sets the first two elements to 1 and 2 (ascii) and the rest to 0. I'm not familiar with the format above. A quick test showed that it's probably just like char arr[10] = {0};, but is there other meaning I'm not aware of?

Augustina
  • 259
  • 1
  • 4
  • 15
  • possible duplicate of [History of trailing comma in programming language grammars](http://stackoverflow.com/questions/2311864/history-of-trailing-comma-in-programming-language-grammars) – Alok Save May 07 '13 at 08:35
  • possible duplicate of [int a\[\] = {1,2,}; Weird comma allowed. Any particular reason?](http://stackoverflow.com/questions/7043372/int-a-1-2-weird-comma-allowed-any-particular-reason) – hammar May 07 '13 at 08:37
  • Just redundant code. can be substituted by arr[10] = {}; – Nikolai May 07 '13 at 08:37
  • Possible duplicate with http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c – MOHAMED May 07 '13 at 08:40
  • Refer to *initializer-clause* in the standard. – devnull May 07 '13 at 08:42

5 Answers5

9

From How to initialize all members of an array to the same value?:

Initialize all members to the same value:

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

Elements with missing values will be initialized to 0:

int myArray[10] = { 1, 2 }; //initialize to 1,2,0,0,0...

So this will initialize all elements to 0:

int myArray[10] = { 0 }; //all elements 0

In C++, an empty initialization list will also initialize every element to 0:

int myArray[10] = {}; //all elements 0 in C++

Objects with static storage duration will initialize to 0 if no initializer is specified:

static int myArray[10]; //all elements 0

If your compiler is GCC you can use following syntax:

int array[1024] = {[0 ... 1023] = 5};
int A[10] = {[0 ... 4] = 5, [5 ... 9] = 3};
Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
6

Yes, it's equivalent with the version without the trailing comma.

See this question for more discussion about trailing commas.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
2

As standard

A trailing comma may appear after the last expression in an array initializer and is ignored

Dayal rai
  • 6,548
  • 22
  • 29
2

There is no difference between int arr[3] ={0,}; and int arr[3] ={0};.

Ref: C11 6.7.9:

    initializer:
         assignment-expression
         { initializer-list }
         { initializer-list , }

Both forms of initializer lists are considered initializers. The form with the comma at the end is preferred by many because it makes it easier to rearrange or add elements to the list during code maintenance.

 int arr[3] ={0,}; 

declares an array of three elements and initializes the first element to 0. When you do a partial initialization, the rest of the array is automatically initialized with zeros.

Ref. C11 6.7.9:

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.

[I wrote this answer for another question, but it got dup closed. I liked the answer though, so I'm reposting on the original question.]

Community
  • 1
  • 1
evaitl
  • 1,365
  • 8
  • 16
1

char arr[10] = { 0, }; and char arr[10] = {0} is same in this case.

But char arr[10] = {5, } is different. 5 will be stored in a[0] and remaining will be filled with zero.

I suggest not to use this for global variables, because it will increase the data section size.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63