0

What I'm Trying To Do

Basically, I've got several possible arrays that I define with macros:

#define ARRAY_ONE  {0, 2, 7, 8}
#define ARRAY_TWO  {3, 6, 9, 2}
#define ARRAY_THREE  {3, 6, 4, 5}
//etc...

At runtime, I have a C-Array that gets used in a lot of places in a certain class. I want this array to use one of the #define values, i.e:

int components[4];

if (caseOne)
{
    components = ARRAY_ONE;
}
else if (caseTwo)
{
    components = ARRAY_TWO;
}
else if (caseThree)
{
    //etc...
}

-

The Problem

However, the above code does not work. Instead, I get a weird error

Expected expression before '[' token

Would anyone mind explaining what's going on, and how I could achieve what I'm attempting to? Any help would be much appreciated - Thanks!

3 Answers 3

4

I don't think that C arrays can be initialized using the curly-brace syntax after they've been declared. You can only do that when initializing them while declaring them.

Try adjusting the previously posted answer with:

const int ARRAY_ONE[] = {0, 2, 7, 8};
const int ARRAY_TWO[] = {3, 6, 9, 2};
const int ARRAY_THREE[] = {3, 6, 4, 5};

int *components;
if (case1) {
    components = ARRAY_ONE;
} else if (case2) {
    components = ARRAY_TWO;
} else if (case3) {
    components = ARRAY_THREE;
}
Sign up to request clarification or add additional context in comments.

3 Comments

That's entirely true. The most obvious solution that I can think of is to use memcpy to copy the required array into components.
Ah! I see what the previous answer was trying to say now, and using pointers as above seems to be a good solution in my case. Thanks!
More precisely, variables in C can't be initialized except at the time they're declared. After that, you're simply assigning them, and you can't assign a C array.
0

I can't really work out what the error is. I suspect it might be coming from some code you haven't posted. Does it say the error is on the int components[4]; line?

Would this do? I uses constants instead of defines.

const int ARRAY_ONE[] = {0, 2, 7, 8};
const int ARRAY_TWO[] = {3, 6, 9, 2};
const int ARRAY_THREE[] = {3, 6, 4, 5};

int* components = ARRAY_ONE;

int whatever = components[2];

1 Comment

Thanks for the reply! The error is on the components = ARRAY lines. Your method doesn't seem to account for the fact that components could be either Array one, two, or three - this is where I'm having trouble.
0

try this:

int ARRAY_ONE[] = {0,2,7,8};

int ARRAY_TWO [] = {3,6,9,2};

int ARRAY_THREE[] = {3,6,4,5};

int components[4];

int count =sizeof(components)/4 //this will get array length, or you can just put array lenght ;

if (case1)

   for (int i =0; i< count; i++)
      components[i] = ARRAY_ONE[i];

else if (case2)

   for (int i =0; i< count; i++)
      components[i] = ARRAY_TWO[i];

Comments

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.