Assign values of a single array to a two-dimensional array of overall equal size

I would like to load the individual values stored in an array of 6 EEPROM addresses to 6 values that are divided into a two dimensional array of [2][3].

I’m using a simplified sketch posted below to test out ‘aligning’ the addresses of the value named EEPROM (dummy name, not actually EEPROM) to the respective addresses in the two-dimensional array.

Ideally, the goal would be that the numbers 1-6 in both “TWODArray” and “EEPROM” would be in lock step in the serial monitor. That way each individual address matches up to the location it’s being loaded to, none of them ever doubling up. However, I’m beginning to think that having 2 nested for loops isn’t what I should be doing at all since the for loop must do exactly that: loop, before the next value advances.

I appreciate any help and advice. Please let me know if I need to clarify anything. Thank you.

``````int TWODArray[2][3] = {
{1, 2, 3},
{4, 5, 6}};

int EEPROM[6] = {1, 2, 3, 4, 5, 6};

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

void loop() {
for(int i = 0; i < 6; i++){
for(int j = 0; j < 2; j++){
for(int k = 0; k < 3; k++){
Serial.print("TWODArray: ");
Serial.println(TWODArray[j][k]);
Serial.print("EEPROM: ");
Serial.println(EEPROM[i]);
delay(400);
}
}
}
}
``````

Get rid of the for loop for i, you want to base the index of EEPROM off the the same variables as the index of TWODArray.

``````      Serial.println(EEPROM[j * 3 + k]);
``````

If you want to store or retrieve the entire array TWODArray to EEPROM, use the .put() and .get() functions, that will work with the entire array instead of individual elements.

``````int TWODArray[2][3];
int EEPROM[6] = {1, 2, 3, 4, 5, 6};

void setup()
{
Serial.begin(115200);
while (!Serial);
memcpy(TWODArray, EEPROM, sizeof(EEPROM));
//to prove that it worked
for (int row = 0; row < 2; row++)
{
for (int col = 0; col < 3; col++)
{
Serial.print(TWODArray[row][col]);
Serial.print(" ");
}
Serial.println();
}
}

void loop()
{
}
``````

That's awesome, thank you! Really liking the memcpy format but I'm having a difficult time figuring out how to do the EEPROM.read now. I'm very confused about when brackets should be used to indicate an array (loadVal[2][3]) vs when to just use it as a pointer. because I'm getting an incompatible types error when using this formula:

``````  memcpy(TWODArray, EEPROM, sizeof(EEPROM));
``````

It sounds like I have to use brackets in which case I'd have to make a jungle of for loops anyway, which this whole formula is made to circumvent. Sorry I'm having so much trouble with this.

Nevermind. Pretty sure what needs to happen is that I need a third variable. if EEPROM is the address, then I'd need an array of 6 variables (let's call it, ONEDarray), and then do the memcpy.

No need for the memcpy, you can read/write the entire array to EEPROM using put() and get().

``````#include <EEPROM.h>

int TWODArray[2][3];
int testdata[6] = {2, 3, 5, 7, 11, 13};

void setup() {
Serial.begin(9600);
EEPROM.put(0, testdata); //write testdata array to EEPROM starting at address 0
}

void loop() {
Serial.println("Start of loop");
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
Serial.print("TWODArray: ");
Serial.println(TWODArray[j][k]);
delay(400);
}
}
}
``````

Why isn't the data in a 2 dim array in the first place ?