0

I have four arrays that contains some values. Another array should contain all these four arrays like shown in the code:

static const long ONE_COLOR[2] = { RGB_BLACK, RGB_WHITE };
static const long TWO_COLOR[4] = { RGB_WHITE, RGB_RED, RGB_GREEN, RGB_BLUE };
static const long THREE_COLOR[8] = { RGB_BLACK, RGB_RED, RGB_GREEN, RGB_BLUE,
    RGB_CYAN, RGB_YELLOW, RGB_MAGENTA, RGB_WHITE };
static const long FOUR_COLOR[16] = { RGB_WHITE, RGB_RED, RGB_GREEN, RGB_BLUE,
    RGB_CYAN, RGB_YELLOW, RGB_MAGENTA, RGB_DARK_RED, RGB_DARK_GREEN,
    RGB_DARK_BLUE, RGB_LIGHT_BLUE, RGB_LIGHT_GREEN, RGB_ORANGE, RGB_LIME,
    RGB_PINK, RGB_LILA };

//this array should contain all other arrays
static const long COLOR_ARRAY = {ONE_COLOR,TWO_COLOR, THREE_COLOR,
    FOUR_COLOR };

My problem is to access the values in the array. I thought I can receive the value of RGB_BLACK with COLOR_ARRAY[0][0]. I tried it with some pointer constructions, but it doesn't work neither :(

1
  • Why isn't COLOR_ARRAY declared as an array? You call it "array" in the comments. You used ARRAY in the variable name. But it is declared as an ordinary non-array variable. Why? You do know that array declaration requires [], don't you? So, where's [] in COLOR_ARRAY declaration? Commented May 9, 2013 at 23:54

4 Answers 4

4

It sounds like you want an array of pointers to arrays.

static const long *const COLOR_ARRAY[4] = {
    ONE_COLOR, TWO_COLOR, THREE_COLOR, FOUR_COLOR
};

Both const are recommended: the first const means that this is a pointer to constant arrays, the second const means that this array of pointers is, itself, constant.

You can access the elements as you'd think, so COLOR_ARRAY[1][3] == RGB_BLUE, et cetera.

Note

I am being a little sloppy with terminology. You are not actually getting pointers to arrays but pointers to the first element in each array. For most operations, in C, an array and a pointer to the first element are interchangeable.

3
  • m( I thought to complicated. Thanks for your hint! :) Commented May 9, 2013 at 23:45
  • 2
    @RichardJ.RossIII it does exactly what it ought to do — whether your expectations are up to the task is another matter. :)
    – hobbs
    Commented May 10, 2013 at 0:07
  • @RichardJ.RossIII: One could use that reason to argue that no programming language should exist, because programs never do what you want, only what you specify. Commented May 10, 2013 at 0:26
0

Maybe you mean

static const long COLOR_ARRAY[] = {ONE_COLOR,TWO_COLOR, THREE_COLOR, FOUR_COLOR };

You don't have [] in your version.

0

you can define COLOR_ARRAY like this:

static const long* COLOR_ARRAY[4] = {ONE_COLOR,TWO_COLOR, 
                                     THREE_COLOR,FOUR_COLOR };

now you should be able to use:

COLOR_ARRAY[0][0]
1
  • m( I thought to complicated. Thanks for your hint! :) Commented May 9, 2013 at 23:45
-1

You can combine arrays like this:

first[3] = { 11, 17, 23 };
second[3] = { 46, 68, 82 };

combo[2][3] = {
         { 11, 17, 23 },{ 46, 68, 82 }};

I shamelessly copied this example from here: http://rapidpurple.com/blog/tutorials/c-tutorials/programming-in-c-array-of-arrays/

Check it out!

Best regards -Tom

1
  • This is hardly helpful, and violates the original license on the code you posted. Commented May 9, 2013 at 23:53

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.