1

I am using this one tiny bit of code:

CGFloat gradientLocations[2] = {1.0f, 0.0f};

which unfortunatelly has a bug, because the gradient points at one direction and is not correctly rotated. So I wanted to fix it with azimuth like this:

CGFloat gradientLocations[2] = self.isAzimuthDown ? {0.0f, 1.0f} : {1.0f, 0.0f};

But I keep getting error that I am missing ":", which I don't believe I am...my question is - what is wrong with it and how to fix it?

3
  • Just searched on google and found this stackoverflow.com/q/15877560/468724 Jul 1, 2014 at 9:27
  • I searched for something quite different as I thought I am accessing an item in array on index no.2 (I know very nooby mistake). So I didn't actually know, that what I am looking for is "Array initializer" related topic. Also I googled xcode, not plain C...
    – Michal
    Jul 1, 2014 at 9:32
  • Anyway, thanks for the heads-up @InderKumarRathore
    – Michal
    Jul 1, 2014 at 9:34

1 Answer 1

1

The language does not support conditional expressions in array initializers. You can fix this by using memcpy, or by using conditional expressions inside a single initializer for each individual element:

Using memcpy (demo):

CGFloat gradientLocations[2];
memcpy(gradientLocations, self.isAzimuthDown ? (CGFloat[]){0.0f, 1.0f} : (CGFloat[]){1.0f, 0.0f}, sizeof(gradientLocations));

Using conditional in scalar expressions (demo):

CGFloat gradientLocations[2] = {self.isAzimuthDown ? 0.0f : 1.0f, self.isAzimuthDown ? 1.0f : 0.0f};
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.