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?

Michal
  • 15,429
  • 10
  • 73
  • 104
  • Just searched on google and found this http://stackoverflow.com/q/15877560/468724 – Inder Kumar Rathore Jul 01 '14 at 09: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 01 '14 at 09:32
  • Anyway, thanks for the heads-up @InderKumarRathore – Michal Jul 01 '14 at 09:34

1 Answers1

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};
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523