Array initialization C
Asked Answered
A

5

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?

Asco answered 7/5, 2013 at 8:31 Comment(5)
possible duplicate of History of trailing comma in programming language grammarsJeweller
possible duplicate of int a[] = {1,2,}; Weird comma allowed. Any particular reason?Cecrops
Just redundant code. can be substituted by arr[10] = {};Athalia
Possible duplicate with #201601Sumach
Refer to initializer-clause in the standard.Quadruple
P
6

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

See this question for more discussion about trailing commas.

Pancreatin answered 7/5, 2013 at 8:33 Comment(0)
S
10

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};
Sumach answered 7/5, 2013 at 8:41 Comment(0)
P
6

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

See this question for more discussion about trailing commas.

Pancreatin answered 7/5, 2013 at 8:33 Comment(0)
C
2

As standard

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

Chuff answered 7/5, 2013 at 8:48 Comment(1)
Please provide a link to standard (a small google request shows that this line is quoted from Java standard).Decurrent
R
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.]

Runkle answered 20/7, 2016 at 16:32 Comment(0)
S
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.

Sprage answered 7/5, 2013 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.