How to clone multidimensional array to another array Arduino

Hello everyone :slight_smile: ,

I’m working with a multidimensional array [4][8] FourRowsEightColums and I have been trying to copy all values array to another array[4][8] with the exactly the same structure FourRowsEightColums.

char ArrayA[4][8] ={
  {'H','-','L','-','H','-','L','-'},
  {'-','H','-','L','-','H','-','L'},
  {'H','-','L','-','H','-','L','-'},
  {'-','H','-','L','-','H','-','L'}};

char AssyB[4][8] ={
  {'L','-','H','-','L','-','H','-'},
  {'-','L','-','H','-','L','-','H'},
  {'L','-','H','-','L','-','H','-'},
  {'-','L','-','H','-','L','-','H'}};

char AssyC[4][8] ={
  {'-','H','-','L','-','H','-','L'},
  {'L','-','H','-','L','-','H','-'},
  {'-','H','-','L','-','H','-','L'},
  {'L','-','H','-','L','-','H','-'}};

char ArraySelected[4][8];
caar ArrayRead[4][8];

I have created a function to compare a values read array from Hall-Sensors-Magnetics vs to ArrayA, B and C but I need copy the array A, B or C depending of the first value to ArrayRead to after compare with ArraySelected and not repit FORs into IFs.

bool CompareResults(){
  bool Good;
    if(AssyReader[0][0] == 'L'){
    ArraySelected = AssyA;
  } else if(AssyReader[0][0] == 'H'){
    ArraySelected = AssyB;
  } else (AssyReader[0][0] == '-'){
    ArraySelected = AssyC;
  }


  for(int j=0;j<=3;j++){
    for(int i=0;i<=7;i++){
      if(AssyReader[j][i]== AssySlected[j][i]){
        Good = true;
      } else {
        Good = false;
        break;
      }
    }
  }
  return Good;
}

I have errors "invalid array assugment" to copy the Array A, b and C to ArraySelected.

Does anyone know how I can solve this without introducing the FORs inside each IF..

use memcpy( arr1, arr2, sizeof(arr1)) to copy everything

blh64:
use memcpy( arr1, arr2, sizeof(arr1)) to copy everything

Or (depending on the useage/need) you could create a pointer and copy the array's pointer value. Saves some memory...

blh64:
use memcpy( arr1, arr2, sizeof(arr1)) to copy everything

:slight_smile: It works, I have adjusted the code for this example for the rest of users :o .

char AssyA[4][8] ={
  {'H','-','L','-','H','-','L','-'},
  {'-','H','-','L','-','H','-','L'},
  {'H','-','L','-','H','-','L','-'},
  {'-','H','-','L','-','H','-','L'}};

char AssyB[4][8] ={
  {'L','-','H','-','L','-','H','-'},
  {'-','L','-','H','-','L','-','H'},
  {'L','-','H','-','L','-','H','-'},
  {'-','L','-','H','-','L','-','H'}};

char AssyC[4][8] ={
  {'-','H','-','L','-','H','-','L'},
  {'L','-','H','-','L','-','H','-'},
  {'-','H','-','L','-','H','-','L'},
  {'L','-','H','-','L','-','H','-'}};

char ArraySelected[4][8];
char ArrayRead[4][8] = {
  {'H','-','L','-','H','-','L','-'},
  {'-','H','-','L','-','H','-','L'},
  {'H','-','L','-','H','-','L','-'},
  {'-','H','-','L','-','H','-','L'}};


  
bool Good;

void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    // read the incoming byte:
    ArrayRead[0][0] = Serial.read();

    Serial.println(ArrayRead[0][0]);

    if(ArrayRead[0][0] == 'L')
    {
      memcpy(ArraySelected, AssyA, sizeof(ArraySelected));
    } 
    else if(ArrayRead[0][0] == 'H')
    {
      memcpy(ArraySelected, AssyB, sizeof(ArraySelected));
    } 
    else if(ArrayRead[0][0] == '-')
    {
      memcpy(ArraySelected, AssyC, sizeof(ArraySelected));
    } 
    
  
    for(int j=0;j<=3;j++){
      for(int i=0;i<=7;i++){
        Serial.print(ArraySelected[j][i]);
        /*if(ArrayRead[j][i]== ArraySelected[j][i]){
          Good = true;
        } else {
          Good = false;
          break;
        }*/
      }
    }
  }

After you copy the array, do you ever need to change the copy? If not, then this is quite wasteful of RAM.

If it doesn't change, not only can you use a pointer that just points to the array you want to use, you could ALSO move it into PROGMEM for even more memory savings.

The original sketch used 348 bytes RAM, the PROGMEM / pointer version uses 190 bytes RAM.

const char AssyA[4][8] PROGMEM = {
    {'H', '-', 'L', '-', 'H', '-', 'L', '-'},
    {'-', 'H', '-', 'L', '-', 'H', '-', 'L'},
    {'H', '-', 'L', '-', 'H', '-', 'L', '-'},
    {'-', 'H', '-', 'L', '-', 'H', '-', 'L'}
};

const char AssyB[4][8] PROGMEM = {
    {'L', '-', 'H', '-', 'L', '-', 'H', '-'},
    {'-', 'L', '-', 'H', '-', 'L', '-', 'H'},
    {'L', '-', 'H', '-', 'L', '-', 'H', '-'},
    {'-', 'L', '-', 'H', '-', 'L', '-', 'H'}
};

const char AssyC[4][8] PROGMEM = {
    {'-', 'H', '-', 'L', '-', 'H', '-', 'L'},
    {'L', '-', 'H', '-', 'L', '-', 'H', '-'},
    {'-', 'H', '-', 'L', '-', 'H', '-', 'L'},
    {'L', '-', 'H', '-', 'L', '-', 'H', '-'}
};

//char ArraySelected[4][8];
//char ArrayRead[4][8] = {
//    {'H', '-', 'L', '-', 'H', '-', 'L', '-'},
//    {'-', 'H', '-', 'L', '-', 'H', '-', 'L'},
//    {'H', '-', 'L', '-', 'H', '-', 'L', '-'},
//    {'-', 'H', '-', 'L', '-', 'H', '-', 'L'}
//};


//  The declaration syntax looks horrible, but you can use it just like a 2D array.
const char (*pSelected)[8] = nullptr;

//bool Good;

void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
}

void loop() {
    if (Serial.available() > 0)
    {
        // read the incoming byte:
        //ArrayRead[0][0] = Serial.read();
        char c = Serial.read();

        Serial.println(c);

        pSelected = nullptr;
        
        if (c == 'L')
        {
            pSelected = AssyA;
            // memcpy(ArraySelected, AssyA, sizeof(ArraySelected));
        }
        else if (c == 'H')
        {
            pSelected = AssyB;
            // memcpy(ArraySelected, AssyB, sizeof(ArraySelected));
        }
        else if (c == '-')
        {
            pSelected = AssyC;

            // memcpy(ArraySelected, AssyC, sizeof(ArraySelected));
        }

        if (pSelected != nullptr)
        {
            for (int j = 0; j <= 3; j++) {
                for (int i = 0; i <= 7; i++) {
                    Serial.print((char)pgm_read_byte(&(pSelected[j][i])));
                    //Serial.print(ArraySelected[j][i]);
                    /*  if(ArrayRead[j][i]== ArraySelected[j][i]){
                        Good = true;
                        } else {
                        Good = false;
                        break;
                        }*/
                }
            }
        }
        else
            Serial.println(F("No Selection."));
    }
}

MHotchin thks so much, I can see the execution a little bit more slow during the reading process. I included your recommendation “PROGMEM” to release RAM during the execution and this is going great.