4

This is probably a dumb questions. I'm modifying a code developed by someone else. I need to particularize the value of some chars array based on a logic variable ThreeDim. I'm trying to do this without success.

int         VarNumber  = ThreeDim==1 ? 3 : 2;
const char* VarList [] = ThreeDim==1 ? {"X","Y","Z"} : {"X","Y"};

But the compiler is giving me errors like

error: expected ‘;’ before ‘}’ token
error: initializer fails to determine size of ‘VarList’

VarList needs to be a const char* due to downstream requirements. And its size should be VarNumber. Thanks

0

4 Answers 4

2

You might consider using the preprocessor, #define THREE_DIM, and then use #ifdef to select one or the other code to compile:

#define THREE_DIM

#ifdef THREE_DIM
int         VarNumber  = 3;
const char* VarList [] = {"X","Y","Z"};
#else
int         VarNumber  = 2;
const char* VarList [] = {"X","Y"};
#endif
1
  • At the end this was the solution I used. The one that presented less syntax issues.
    – phollox
    Commented May 2, 2015 at 0:37
2

I don't think you can do this with different sized array initializers. However, you can put a conditional expression in the initializer:

const char* VarList [] = {"X", "Y", ThreeDim == 1 ? "Z" : nullptr};

This will always give you a 3 element array with the last element either "Z" or a null pointer.

3
  • Thanks. It compiles now. Thanks again... Now, I just have to solve the SIGSEGV :( Legacy code...
    – phollox
    Commented May 1, 2015 at 3:48
  • 3
    Mind the last null element. Could as well be the cause of the segfault.
    – vitaut
    Commented May 1, 2015 at 3:54
  • Thanks @vitaut. At the end, I used the solution proposed by @GregHewgill. And compiled with and without the #define line, generating different executables for each case. Thanks
    – phollox
    Commented May 2, 2015 at 0:37
2

No, because e.g. {"X","Y","Z"} is not an expression. It's handled specially by the compiler for variable definitions.

And if you're programming C++ you should be using std::vector and std::string instead, then it would be very simple (but verbose):

std::vector<std::string> VarList = ThreeDim==1 ?
    std::vector<std::string>{{"X","Y","Z"}} : 
    std::vector<std::string>{{"X","Y"}};
1
  • Thanks for the answer. And agree, the type of object is far from optimal. But as I said, the code is not mine, and I can't change the object type without significant modifications downstream
    – phollox
    Commented May 1, 2015 at 3:44
1

As per C11 6.7.9, arrays aren't initialized with an expression but with an initializer list; these are two different syntactical elements and can't be mixed. This derives from the fact that arrays aren't assignable, and treating one as a value (as opposed to a value that decays to a pointer) therefore doesn't make sense. Since they aren't values, there's no way to create a legal expression that passes around whole arrays.

The closest direct analogue to what you're asking for would be:

const char** VarList = ThreeDim==1 ? (const char*[]){"X","Y","Z"} : (const char*[]){"X","Y"};

...but this comes at the cost of changing the type of VarList, and is probably not what you want.

2
  • Nice idea, but on ideone.com this gives "error: taking address of temporary array", which seems about right...? Commented May 1, 2015 at 3:52
  • 1
    @TonyD in C taking the address of a temporary array isn't an error, I guess that's yet another C/++ difference. Commented May 1, 2015 at 3:59

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.