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.
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.
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'.
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.
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 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.
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.
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.};
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.
© 2022 - 2024 — McMap. All rights reserved.
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 ofint myArray[10] = {}
? – Hein