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.

Rad'Val
  • 8,895
  • 9
  • 62
  • 92
  • @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] = {}`? – Rad'Val Oct 25 '11 at 18:37
  • 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. – mloskot Oct 25 '11 at 18:42
  • 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. – mloskot Oct 25 '11 at 18:52

6 Answers6

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'.

Bee San
  • 2,605
  • 2
  • 20
  • 19
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.

Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181
  • The accepted answer in this: http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c 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? – Rad'Val Oct 25 '11 at 18:42
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.

John Bode
  • 119,563
  • 19
  • 122
  • 198
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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
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.};
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
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.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138