Initialise all elements in an array with a value using compound literals
Asked Answered
C

6

8
float myArray[myArraySize] = {1};

In the expression above only the first element is init with 1. How can you init all the elements with a value using a compound literals(not memset)?

I'm using GCC 4.2 on unix to compile.

Cullin answered 25/10, 2011 at 18:30 Comment(3)
@mloskot the question/answer you suggested states that int myArray[10] = { 0 }; //all elements 0 what's different in my case? if you can only init with zero, why bother to write that instead of int myArray[10] = {}?Hein
If you have read the answers to this question, especially the highest scored one, you'd learn what behaviour to expect. So, I judged it's duplicate.Chroma
Valentin, I assumed you mean C as C89, so I skipped the features of C99. So, I withdraw the suggestion it is a duplicate. If we consider C99, then it is a distinct question.Chroma
P
6

This

float myArray[100] = {[0 ... 99] = 1.0};

is how you do it.

See Designated Initializers in the GCC docs which says:

To initialize a range of elements to the same value, write `[first ... last] = value'.

Phan answered 25/10, 2011 at 18:38 Comment(3)
Argh, I forgot about this! I knew I read it somewhere in the past! Thanks!Hein
+1, but note that this is a gnu extension and will not work with all compilersBareback
@WilliamPursell True, but he says he's using GCC 4.2, so it works for him.Phan
A
5

No, only the first element will be initialized to 1.0. The rest will be initialized, but to 0.0 per the C standard. Have a look at the C faq for some more examples.

Aeciospore answered 25/10, 2011 at 18:31 Comment(1)
The accepted answer in this: #201601 question states that you can do int myArray[10] = { 0 }; to set all elements to 0. Is the answer wrong and the elements are 0 anyway? Or it only works with 0 value?Hein
D
2

From the language standard:

6.7.8 Initialization
...
10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. 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.
...
21 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.

int arr[10]={1} and int arr[10]={0} produce exactly the same result - element 0 is initialized to whatever is specified between the braces, and elements 1 through 9 are initialized to 0 based on paragraphs 10 and 21 above. It only appears to be different because in one case the explicit initializer is the same as the implicit initializer.

Durban answered 25/10, 2011 at 20:57 Comment(0)
E
0

No you need to do them all. Those that don't get specified are given a default value, 0f for a float.

float myArray[4] = {1, 1, 1, 1};

You may find that a simple for loop will result in the most maintainable code. I certainly would not encourage the use of memset here.

Etana answered 25/10, 2011 at 18:32 Comment(0)
E
0

How can you init all the elements with a value using this formula (not memset)?

With a loop. You can't do this in a sane and portable way with memset.

float myArray[myArraySize] = {1};
for (size_t i=0; i<myArraySize; i++)
    myArray[i] = 1.;

It may seem ugly, but it's the C way (and it gets prettier if you rename myArraySize to something like N). Alternatively, if the number of elements is fixed, you can just enumerate the array, and optionally leave off the size:

float myArray[] = {1., 1., 1., 1., 1., 1.};
Electrolier answered 25/10, 2011 at 18:33 Comment(0)
G
0

Initializing any array in modern versions of C will initialize all non-specified entries to zero.

To initialize all non-specified entries to a different value, you can try memset; however, that only works very well on char arrays.

For non-char arrays, you have to write a loop.

Gluey answered 25/10, 2011 at 18:33 Comment(3)
"modern" meaning anything since 1989; you're not likely to find a C compiler that doesn't do this correctly.Abaca
@Keith, True, but now you've made me feel old. :)Gluey
You feel old? My heart bleeds. 8-)}Abaca

© 2022 - 2024 — McMap. All rights reserved.