Two dimensional array question

Hello,

I'm prototyping a program which will cycle through a series of RGB values and assign them to a RGB LED. I got stuck trying to setup a two dimensional array. I saw a few previous posts on multi-dimensional arrays on this board but still wasn't able to get things working. Here's what I have so far...

int color1[] = {140, 40, 00}; // Orange int color2[] = {200, 20, 00}; // Red int color3[] = {100, 255, 100}; // Green int color4[] = {10, 120, 30}; // Greener int color5[] = {6, 1, 34}; // Dark

int color_set[] = {color1, color2, color3, color4, color5};

I get "error: invalid conversion from 'int*' to 'int'" on compile.

Thanks for any help you can give!

int* color_set[] = {color1, color2, color3, color4, color5};

The error message had a hidden clue in it ;)

When you use arrays by name, like in your definition of color_set[], the name is essentially a pointer. This is why you are getting the error as you state.

Easiest fix is to simply declare color_set as:

int *color_set[] = {color1, color2, color3, color4, color5};

OR

remove your individual color definitions and define color_set as

   int color_set[][3] = {
        {140,40,0}, // Orange
        {200,20,0}, // Red
        {100,255,100}, // Green
        {10,120, 30}, // Greener
        {6,1,34} // Dark
   };

// either way will allow you to use color_set as below:

    printf("color1={%d,%d,%d}\n",
           color_set[0][0], color_set[0][1], color_set[0][2]);

-Rusty-

What Rusty said- what you've got here:

int* color_set[] = {color1, color2, color3, color4, color5};

is an Iliffe vector, not a 2D array, though the syntax for access is identical.

Iliffes are handy for creating non-square arrays.

Thanks for all the great info. Int* worked! One last question.. What does the * signify? I searched for "Iliffe" on this site but was not able to get a good explanation of what it meant other than 'circular array' and found no usage info.

Thanks again for the great help!

The "" signifies a pointer, so "int" is a pointer to an "int". You've just declared an array of pointers to "int"s, because the name of an array is a pointer to the array (or the address of the first element).

An Iliffe vector (it is a fairly obscure term) is just an array of pointers. You could use it to allow you to access (as here) memory that is potentially non-contiguous as though it were contiguous (you could easily (and maliciously!) re-arrange the apparent order of the "rows" of "set_color" simply by re-assigning the pointers), or as said earlier, for implementing non-rectangular arrays. Because the size of the vectors attached to "color_set" isn't specified, they can individually be of any length.

http://en.wikipedia.org/wiki/Iliffe_vector

Something to keep in mind:

byte array1[2] = { 1, 2 };
//declare some other variables here,
byte array2[2] = { 3, 4 };
//...here...
byte array3[2] = { 5, 6 };
//...or here
byte* allArrays[3] = {array1, array2, array3};

Will cause allArrays[1][0] to be the exact same as array2[0].

And you are not guaranteed that the adress of allArrays[0][0] , allArrays[0][1] , allArrays[1][0] , allArrays[1][1] to be in sequence. This can bite you in the ass if you try to do any trickery based upon the assumption that the adresses are in sequence.

Imagined layout in RAM ([x] indicates a cell/adress in the RAM)

[1][2][a][b][3][4][c][d][e][5][6]

However, this:

byte allArrays[3][2] = {
  {1,2},
  {3,4},
  {5,6}
};

Will cause the data to be in perfect sequence in RAM.

Imagined layout in RAM ([x] indicates a cell/adress in the RAM)

[1][2][3][4][5][6]

Generally, I think it's best to use 2d arrays unless you need to have different lengths on the 1st dimension. :)