# How do I assign one array to another dynamically?

Hello,

I have a set of over 50 different arrays, and I need to be able to reference them in a simple way so that I can minimize the code (obviously the goal, right?)

So here is an example of the core arrays:

``````unsigned int aryMenu[] = {4400, 4400, 550, 1650, 550, 1650, 550, 1650, 550, 550, 550, 550, 600, 550, 550, 550, 550, 550, 550, 1650, 550, 1650, 550, 1650, 550, 550, 550, 600, 550, 550, 550, 500, 600, 550, 550, 600, 550, 1650, 550, 550, 550, 1650, 550, 1650, 550, 550, 550, 550, 550, 600, 550, 1650, 550, 550, 550, 1650, 550, 550, 550, 550, 550, 1650, 550, 1650, 550, 1650, 550};
unsigned int aryGuide[] = {3300,3350,800,2550,750,2550,800,850,800,2500,850,2500,800,900,800,2500,800,2550,800,850,800,850,800,850,800,900,750,900,750,2550,800,900,750,850,800,2550,800,850,800,850,800,2550,800,2500,800,2550,800};
unsigned int aryOK[] = {3250,3350,800,2500,850,2500,800,900,750,2550,800,2550,800,850,800,850,800,2550,800,2500,800,850,800,900,750,900,800,800,850,2500,800,850,800,900,800,2500,800,2550,800,900,750,850,800,2550,800,2500,800};
``````

Then I have an enum like this:

``````enum CMDS{
ARYGUIDE,
ARYOK
}
``````

And this is how I would LIKE to be able to reference those arrays:

``````void doCommand(unsigned int option[]) {
//do whatever with passed array
}

void runCommand(CMDS cmds) {

unsigned int option[];

switch (cmds) {
break;

case ARYGUIDE:
option = aryGuide;
break;

case ARYOK:
option = aryOK;
break;
}

doCommand(option);

}
``````

But i'm finding it to be not possible to assign one of the pre-defined arrays to a variable array which can then be thrown into another function...

Is there a way to do this?

Thank you,

Mike

``````unsigned int *option;
``````

or

``````unsigned int *option;
``````

You'd be well advised to also pass the size of the selected array to your 'doCommand()' function.

gfvalvo:
You'd be well advised to also pass the size of the selected array to your 'doCommand()' function.

I will, and it looks like this is going to work, thank you!

Mike

Since your arrays seem to contain only non-zero values you could put a 0 at the end of the list and have doCommand() count the number of elements up to the 0.

I haven’t counted, but with 50 arrays similar to your examples, that is quite a lot of storage.
What Arduino are you using ?
If the arrays are static, you may be able to put them in progmem.

6v6gt:
I haven’t counted, but with 50 arrays similar to your examples, that is quite a lot of storage.
What Arduino are you using ?
If the arrays are static, you may be able to put them in progmem.

I'm glad you asked! ... I was just going to start another thread on this because when I went to compile the completed code with the library the way I wanted it... the compiler choked on it ... said I was using like 348% of available memory .... my other thread will lay out what Im actually trying to accomplish ... hoping to find a solution that will work for me.

johnwasser:
Since your arrays seem to contain only non-zero values you could put a 0 at the end of the list and have doCommand() count the number of elements up to the 0.

Another way is to let the compiler calculate the number of element.

``````unsigned int *option;
byte option_size; //store the number of elements in the array

``````

Instead of assigning an array to another array, you could create an array of pointers to the arrays. That would also allow you to use the enum as the index into the array of pointers.

``````const unsigned int aryMenu[] = {4400, 4400, 550, 1650, 550, 1650, 550, 1650, 550, 550, 550, 550, 600, 550, 550, 550, 550, 550, 550, 1650, 550, 1650, 550, 1650, 550, 550, 550, 600, 550, 550, 550, 500, 600, 550, 550, 600, 550, 1650, 550, 550, 550, 1650, 550, 1650, 550, 550, 550, 550, 550, 600, 550, 1650, 550, 550, 550, 1650, 550, 550, 550, 550, 550, 1650, 550, 1650, 550, 1650, 550};
const unsigned int aryGuide[] = {3300, 3350, 800, 2550, 750, 2550, 800, 850, 800, 2500, 850, 2500, 800, 900, 800, 2500, 800, 2550, 800, 850, 800, 850, 800, 850, 800, 900, 750, 900, 750, 2550, 800, 900, 750, 850, 800, 2550, 800, 850, 800, 850, 800, 2550, 800, 2500, 800, 2550, 800};
const unsigned int aryOK[] = {3250, 3350, 800, 2500, 850, 2500, 800, 900, 750, 2550, 800, 2550, 800, 850, 800, 850, 800, 2550, 800, 2500, 800, 850, 800, 900, 750, 900, 800, 800, 850, 2500, 800, 850, 800, 900, 800, 2500, 800, 2550, 800, 900, 750, 850, 800, 2550, 800, 2500, 800};

const unsigned int * aryOption[] = {
aryGuide,
aryOK
};

const byte aryElements[] = {
(sizeof(aryGuide) / sizeof(aryGuide[0])),
(sizeof(aryOK) / sizeof(aryOK[0])),
};

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

void loop() {
for (byte i = 0; i < (sizeof(aryOption) / sizeof(aryOption[0])); i++) {
for (byte j = 0; j < aryElements[i]; j++) {
Serial.print(aryOption[i][j]);
Serial.print(" ");
}
Serial.println();
}
delay(30000);
}
``````