3

So, I can initialize a C array like this:

CGFloat colors[8]= {1,0.5,0.5, 1,
    0.5,0.2,0.2, 1};

What if I want to define colors[8] but assign the 8 values conditionally. Is this possible? If it is, I cannot find the right syntax. Something like this:

CGFloat colors[8];
if (red){
colors= {1,0.5,0.5, 1,
    0.5,0.2,0.2, 1};
}else
 //assign colors to something else

I've tried various syntaxes but nothing works. I'm guessing it is not possible?

4 Answers 4

8

This will not work, as this way of assignment is only allowed at array initialization. what you can do is:

CGFloat colors[8];
if (red){
    CGFloat temp[] = {1,0.5,0.5, 1, 0.5,0.2,0.2, 1};
    memcpy(colors, temp, sizeof(colors));
}
else
{
    // ...
}
Sign up to request clarification or add additional context in comments.

6 Comments

i assume memcpy is required and you cannot just set one array equal to another?
@andrewx: In C, assignment to copy arrays only happens if the arrays are wrapped by equivalent struct/union types.
so if both my arrays are CGFloat I should be able to assign one to the other without using memcpy since they are both of the same struct?
struct Foo { float arr[3]; }; struct Foo a = { { 1., 2., 3. } }; struct Foo b = a;
@user315052 just as an aside, you should probably not create a struct like that just to copy an array, and instead just use memcpy.
|
1

Try something like the following, maybe...

CGFloat colors_red[8] = { 1, 0.5, 0.5, 1, 0.5, 0.2, 0.2, 1};
CGFloat colors_other[8] = ...;
...
const CGFloat *colors = red ? colors_red : colors_other;

If you you need copies of the original data (i.e. you plan on mutating colors but reusing colors_red or colors_other), you should then use memcpy (declared in <string.h>):

CGFloat colors[8];
memcpy(colors, red ? colors_red : colors_other, sizeof colors);

6 Comments

is that asterisk (*) on your third line of code a typo? CGFloat is not an object.
@andrewx no, I declared colors as a pointer to const CGFloat.
I assume memcpy is required and you cannot just set one array equal to another?
You can assign the pointer to point to the array which will be fine so long as you don't plan on mutating colors and reusing the red_colors or other_colors. In the case you do wish to reuse them AND mutate colors, you need to make a copy, hence std::memcpy.
@veer - this is not c++, no need for std::
|
1

If you have recent C compiler, with C99, you could do

CGFloat *const colors =
 red 
 ? (CGFloat[8]){ 1, 0.5, 0.5, 1, 0.5, 0.2, 0.2, 1}
 : (CGFloat[8]){ /* put other values here */ };

observe the const after the * so your pointer would not be modifiable, but your data would. If red is a compile time integer constant, any decent compiler should be able to reserve only one array for that.

I you also know that the elements of the array will never be changed you could add more const

CGFloat const*const colors =
 red 
 ? (CGFloat const[8]){ 1, 0.5, 0.5, 1, 0.5, 0.2, 0.2, 1}
 : (CGFloat const[8]){ /* put other values here */ };

Then a good compiler could (would be allowed to) allocate the two arrays statically, and thus produce something more efficient in case red is not a constant.

3 Comments

this seems similar to @veer's answer, except that the const is in a different place on the line. does that make a difference?
@andrewx, yes and no, observe that in the second one there is even more const. The placement of these const is important, they mean different things. The easiest to see that here is that the const applies to what is on the left. In my first version, only the pointer is protected from being modified not the contents of the array. In the second they are both protected.
Also the use of compound literals avoids that your array can be accessed through other means, they simply have no name to which you could refer. This also ensures that certain optimizations could be done by the compiler.
0

You would need to do something like

if(red) {
    colours[0] = 1;
    colours[1] = 0.5;
    colours[2] = 0.5;
    ...
    colours[7] = 1;
} else {
    ...
}

3 Comments

what kind of syntax is this (besides the British spelling of colors) ? All the sudden you turned it into a function?
i believe he means brackets, not parenthesis. this user should edit his answer for clarification.
Sorry, you're quite correct. It's an object lesson to all and sundry: a warning of the deleterious effects of being forced to program in VB6. You forget what real programming languages look like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.