Reputation: 95
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 :(
Upvotes: 0
Views: 122
Reputation: 5699
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]
Upvotes: 0
Reputation: 542
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
Upvotes: -1
Reputation: 213767
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.
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.
Upvotes: 4
Reputation: 41220
Maybe you mean
static const long COLOR_ARRAY[] = {ONE_COLOR,TWO_COLOR, THREE_COLOR, FOUR_COLOR };
You don't have []
in your version.
Upvotes: 0