Declaring contents of an array!?

Stewart
173
Stewart  

The following is valid.

double v1[10] = {47.73,49.384,49.845,50.167,50.465,50.628,50.857,51.12,51.315,51.565};

The following is not valid.

double v1[10];

v1 = {47.73,49.384,49.845,50.167,50.465,50.628,50.857,51.12,51.315,51.565};

Any ideas how to make it valid? The reason why I ask is that I want to use conditional logic to populate it - eg

double v1[10];

if (someVariable == true)

v1 = {47.73,49.384,49.845,50.167,50.465,50.628,50.857,51.12,51.315,51.565};

else

v1 = {44,50,49.845,50.167,50.465,50.628,50.857,51.12,51.315,51.565};

[Deleted]  

Just simple

if(someVariable == true)

   double v1[10] = {47.73,49.384,49.845,50.167,50.465,50.628,50.857,51.12,51.315,51.565};
else
          v1[10] = {44,50,49.845,50.167,50.465,50.628,50.857,51.12,51.315,51.565};
Stewart
173
Stewart  
Many thanks Tonny - I'll try that :)
William Roeder
24607
William Roeder  
tonny:

Just simple

if(someVariable == true)

   double v1[10] = {47.73,49.384,49.845,50.167,50.465,50.628,50.857,51.12,51.315,51.565};
else
          v1[10] = {44,50,49.845,50.167,50.465,50.628,50.857,51.12,51.315,51.565};
  1. I wouldn't think that would even compile. It does but does it work?
  2. You don't have to declare the size of the array, as it will default to the number of elements in the initialization. double v1[] = {...};
  3. If the above does not work, use
       double v[10];
       double v1[] = {47.73, 49.384, 49.845, 50.167, 50.465, 50.628, 50.857, 51.12, 51.315, 51.565};
       double v2[] = {44,50,         49.845, 50.167, 50.465, 50.628, 50.857, 51.12, 51.315, 51.565, 0.0};
       if(someVariable) ArrayCopy(v, v1);
       else             ArrayCopy(v, v2);

[Deleted]  
WHRoeder:
  1. I wouldn't think that would even compile. It does but does it work?
  2. You don't have to declare the size of the array, as it will default to the number of elements in the initialization. double v1[] = {...};
  3. If the above does not work, use
Test it first before jumping into conclusions.
Stewart
173
Stewart  

It's actually kind of funny that WHRoeder has pretty much written exactly what I had originally coded (including the v1 and v2s), but the real reason for the question was to get rid of all of the ArrayCopys and make it simpler. In actual fact I have about 50 forks in the statement meaning 50 ArrayCopys.

I'm going with Tonny's solution and will see if it works. Thanks guys!

William Roeder
24607
William Roeder  
tonny:
Test it first before jumping into conclusions.
I did, I compiled it and was surprised that it did.
stewart:

It's actually kind of funny that WHRoeder has pretty much written exactly what I had originally coded (including the v1 and v2s), but the real reason for the question was to get rid of all of the ArrayCopys and make it simpler. In actual fact I have about 50 forks in the statement meaning 50 ArrayCopys.

double v1[2][10] = {47.73, 49.384, 49.845, 50.167, 50.465, 50.628, 50.857, 51.12, 51.315, 51.565,
                    44,50,         49.845, 50.167, 50.465, 50.628, 50.857, 51.12, 51.315, 51.565, 0.0};
if (v1[selector][anIndex]) ..
You can go two dimensions.
onewithzachy
1863
onewithzachy  
Correction : Initializing or assign a values to an array, not declaring a content, the book.