Initialize all elements in an array with value using compound literals

float myArray[myArraySize] = {1}; 

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

I am using GCC 4.2 for unix to compile.

+8
c gcc arrays c99 gcc4
source share
6 answers

it

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

how do you do it.

See Assigned Initializers in GCC Docs for:

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

+6
source share

No, only the first item will be initialized to 1.0. The rest will be initialized, but up to 0.0 on standard C. Look at C faq for some examples.

+5
source share

From the language standard :

6.7.8 Initialization
...
10 If an object with automatic storage duration is not explicitly initialized, its value is undefined. If an object with a static storage duration is not explicitly initialized, then:
- if it has a pointer type, it is initialized with a null pointer; - if it has an arithmetic type, it is initialized with a (positive or unsigned) zero; - if it is an aggregate, each member is initialized (recursively) in accordance with these rules.
- if it is a union, the first named element is initialized (recursively) in accordance with these rules.
...
21 If the list enclosed in curly brackets contains fewer initializers than elements or elements of the population or fewer characters in the string literal used to initialize an array of known sizes than there are elements in the array, the rest of the population must be initialized implicitly the same as objects with a static storage duration.

int arr[10]={1} and int arr[10]={0} produce exactly the same result. Element 0 is initialized with everything that is indicated between curly braces, and elements 1 through 9 are initialized to 0 based on points 10 and 21 above. Apparently, this looks different because in one case the explicit initializer is the same as the implicit initializer.

+2
source share

No, you need to do all this. Those that are not set get the default value, 0f for float.

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

You may find that a simple for loop leads to the most supported code. I definitely will not encourage the use of memset here.

0
source share

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

With a loop. You cannot do this in a smart 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 path (and it gets prettier if you rename myArraySize to something like N ). Alternatively, if the number of elements is fixed, you can simply list the array and optionally leave the size:

 float myArray[] = {1., 1., 1., 1., 1., 1.}; 
0
source share

Initializing any array in modern versions of C will initialize all unwritten entries to zero.

To initialize all unspecified entries to a different value, you can try memset; however, this works very well on char arrays.

For char arrays you have to write a loop.

0
source share

All Articles