Multi-dimensional Array Declaration using existing Arrays?

Hi, is it possible to declare a multi-dimensional array from existing arrays like the following:

(I know the following doesn't work).

//Colors
int red[] = {255,0,0};
int green[] = {0,255,0};
int blue[] = {0,0,255};
int pink[] = {255,0,128};
int yellow[] = {255,255,0};
int aqua[] = {0,255,255};


//Palette
int palette[][] = {red, green, blue, pink, yellow, aqua};


//using the palette for something....
for (i=0; i<5: i++) {
    color = CRGB(palette[i][0],palette[i][1],palette[i][2]); 
    }

I know I can make an array of arrays "manually" like this:

int palette[][3] = {{255,0,0},{0,255,0},{0,0,255}};

But wondered if it could be done with existing arrays, too.

Thanks for the help!
J

(I just realized I posted this in the wrong section, I meant to put it in Syntax... I don't see how I cam move it :-()

I am not sure what exactly you are trying to do.
No, you cannot use the name of an array to initialize another array.

.

To hold related values (like red, green, blue colour components) that comprise a larger unit (a colour) you would normally use a struct.

Here is some 3 way example code that might answer your question:

// Using a struct for the colour components

struct Colour {
  byte r;
  byte g;
  byte b;
};

Colour redColour{255, 0, 0};
Colour greenColour{0, 255, 0};

Colour paletteOfColours[] = { redColour, greenColour }; // copies elements into new array memory, duplication == waste of RAM
Colour *paletteOfColourPtrs[] = { &redColour, &greenColour }; // array of pointers to  previously declared Colour's, no duplication, more complicated syntax

void printColour(Colour c) {
  Serial.print("(");
  Serial.print(c.r); Serial.print(",");
  Serial.print(c.g); Serial.print(",");
  Serial.print(c.b);
  Serial.print(")");
}

// Using an array for the colour components

int red[] = {255, 0, 0};
int green[] = {0, 255, 0};
int blue[] = {0, 0, 255};
int pink[] = {255, 0, 128};
int yellow[] = {255, 255, 0};
int aqua[] = {0, 255, 255};

int *palette[] = {red, green, blue, pink, yellow, aqua}; // don't need the & operator since an array will automatically convert to a pointer when required

void printTriplets(int *triplet) {
  Serial.print("(");
  Serial.print(triplet[0]); Serial.print(",");
  Serial.print(triplet[1]); Serial.print(",");
  Serial.print(triplet[2]);
  Serial.print(")");
}

// Test stuff

void setup() {
  Serial.begin(115200);

  Serial.println("paletteOfColours:");
  for (unsigned i = 0; i < sizeof(paletteOfColours) / sizeof(paletteOfColours[0]); i++) {
    printColour(paletteOfColours[i]);
    Serial.println();
  }
  Serial.println();

  Serial.println("paletteOfColourPtrs:");
  for (unsigned i = 0; i < sizeof(paletteOfColourPtrs) / sizeof(paletteOfColourPtrs[0]); i++) {
    printColour(*paletteOfColourPtrs[i]);
    Serial.println();
  }
  Serial.println();

  Serial.println("palette:");
  for (unsigned i = 0; i < sizeof(palette) / sizeof(palette[0]); i++) {
    printTriplets(palette[i]);
    Serial.println();
  }
  Serial.println();
}

void loop() { }

PS: To have a thread moved to a different forum section just click "Report to moderator" and fill in your request.

Arrays of references (&) or pointers (*) can be used to construct arrays based on existing arrays.
References instead of pointers will remove the need for pointer dereferencing in the code.

DrDiettrich:
Arrays of references (&) ... can be used to construct arrays based on existing arrays.

You sure about that?

You aren't?

To my knowledge, there are no arrays of references in C++.

Thank you all for your replies...

ieee488 - I have a project on which I have some controllable LED Strips... Previously, I had created a few lines of arrays with the different colors (the list that I showed in my example). I have a few assemblies of these and wanted each one to light in a different color. Instead of re-writing all of my code for each one, I used the same code for each one, which included all the color arrays and just manually changed the one line in the Fastled section for the color I wanted before uploading to the board.

eg. CRGB(red[0],red[1],red[2]) for the first unit, CRGB(blue[0],blue[1],blue[3]) for the second and so on.

I decided to "upgrade" my project, adding a color-change button to each unit. In my example above, I used a for-loop to demonstrate cycling through the elements from the palette array, but in practice, I have it so every time you press the button, it advances the "palette array" by one to select the color. - I just figured the "for loop" would be less to type in the forum.

Essentially, I was asking the question about putting the "arrays of colors" inside the "palette array" out of curiosity. Originally I figured I could just re-use my existing code by storing the existing color arrays inside of the palette array but saw that it didn't work. I know I've nested arrays like this in other languages (whether or not it is a waste of resources is debatable, of course) but when I couldn't get it to work here, I wondered if it was something I was doing wrong, or just something that couldn't be done.

arduarn - Thank you for the information on structs and pointers... I am familiar with the concept of pointers, but have only used them in one other project. I was under the impression that the use of pointers is for saving resources - It didn't occur to me to even try them.

Same for structs. I am (clearly) not that advanced of a programmer, so though I know what a Structure is, I didn't know what would make it different from an Array in the sense of how it would behave differently than an array.

Just so I understand (or perhaps to point out that I don't :-)) - you are saying that had I used STRUCTS instead of Arrays to define my colors, the structs can be stored in another array? (Or pointed to as you show?)

I am not entirely sure why the Struct behaves differently than an Array (with respect to being nested inside of another array) but I am reading about it now - looks like it has to do with how Arrays are pointers to the beginning of memory locations and Structs are clearly defined memory locations. (am I on the right track here?)

Thank you again for the assistance!
J

roboscan:
Just so I understand (or perhaps to point out that I don't :-)) - you are saying that had I used STRUCTS instead of Arrays to define my colors, the structs can be stored in another array? (Or pointed to as you show?)

Yes.

roboscan:
I am not entirely sure why the Struct behaves differently than an Array (with respect to being nested inside of another array) but I am reading about it now - looks like it has to do with how Arrays are pointers to the beginning of memory locations and Structs are clearly defined memory locations. (am I on the right track here?)

After writing a paragraph of text and realising that it was totally confusing crap, I'll just link to something (c-faq.com) which is hopefully a bit clearer. See some of the other related FAQs too.