Strange behavior writing to multidimensional string array

Hello,

I already created several projects with different Arduinos, but now struggle with a strange behavior writing to a multidimensional string array:
I only write to one specific cell of my array ([1][0]), but the value is set to two cells ([1][0] and[0][2]).

I could not find any similar topic when searching. The issue is also present after downloading with different PCs and also to different Arduinos (Nano as well as Mega).

bool firstRun = true;

void setup() {
  Serial.begin(9600);
  Serial.println("Start");
}

void loop() {
  String respTable[32][2];

  if (firstRun)
  {
    Serial.println("Array before");
    Serial.println(respTable[0][0] + " | " + respTable[0][1] + " | " + respTable[0][2]);
    Serial.println(respTable[1][0] + " | " + respTable[1][1] + " | " + respTable[1][2]);
    TestArrayCall(respTable);
    Serial.println("Array after");
    Serial.println(respTable[0][0] + " | " + respTable[0][1] + " | " + respTable[0][2]);
    Serial.println(respTable[1][0] + " | " + respTable[1][1] + " | " + respTable[1][2]);
    Serial.println();
    firstRun = false;
  }
}

void TestArrayCall(String rTable[][2])
{
  //I expect to only set one cell of the array (Row 1, Column 0)
  //Somehow, this command sets two cells of the array (Row 1, Column 0 AND Row 0 Column 2)
  rTable[1][0] = "Test [1][0]";
}

Here is the Console output, that shows that the text is written in both cells:

Start
Array before
 |  | 
 |  | 
Array after
 |  | Test [1][0]
Test [1][0] |  |

Why is the value set to two different cells? How is it possible to only write to one cell?
Thank you in advance for any hints!

The most obvious problem is that in the TestArrayCall() function you change the value of a local variable, rTable, and do nothing with it. Changing the local array will have no effect on the array used when calling the function.

Thanks for the reply. As far as I understood from other postings, arrays are passed by reference by default:
https://forum.arduino.cc/index.php?topic=42546.0

  • I defined a multidimensional string array at the beginning of loop()
  • I print the first two rows to serial (everything empty
  • I pass this array to TestArrayCall(String[][2]) (by reference as I understand it correctly: passing arrays by reference - Syntax & Programs - Arduino Forum)
  • I change one cell in the referenced array
  • In loop() I print the first two rows to serial (two cells are changed instead of one).

When I also print the values of the array within the call I can see the exact same behavior.
Here is the adapted code with Serial.println in TestArrayCall():

bool firstRun = true;

void setup() {
  Serial.begin(9600);
  Serial.println("Start");
}

void loop() {
  String respTable[32][2];

  if (firstRun)
  {
    Serial.println("loop: Array before");
    Serial.println(respTable[0][0] + " | " + respTable[0][1] + " | " + respTable[0][2]);
    Serial.println(respTable[1][0] + " | " + respTable[1][1] + " | " + respTable[1][2]);
    TestArrayCall(respTable);
    Serial.println("loop: Array after");
    Serial.println(respTable[0][0] + " | " + respTable[0][1] + " | " + respTable[0][2]);
    Serial.println(respTable[1][0] + " | " + respTable[1][1] + " | " + respTable[1][2]);
    Serial.println();
    firstRun = false;
  }
}

void TestArrayCall(String rTable[][2])
{
  //I expect to only set one cell of the array (Row 1, Column 0)
  //Somehow, this command sets two cells of the array (Row 1, Column 0 AND Row 0 Column 2)
  Serial.println("Call: Array before");
  Serial.println(rTable[0][0] + " | " + rTable[0][1] + " | " + rTable[0][2]);
  Serial.println(rTable[1][0] + " | " + rTable[1][1] + " | " + rTable[1][2]);
  rTable[1][0] = "Test [1][0]";
  Serial.println("Call: Array after");
  Serial.println(rTable[0][0] + " | " + rTable[0][1] + " | " + rTable[0][2]);
  Serial.println(rTable[1][0] + " | " + rTable[1][1] + " | " + rTable[1][2]);
  Serial.println();
}

Console Output:

Start
loop: Array before
 |  | 
 |  | 
Call: Array before
 |  | 
 |  | 
Call: Array after
 |  | Test [1][0]
Test [1][0] |  | 

loop: Array after
 |  | Test [1][0]
Test [1][0] |  |

=> In loop() as well as in TestArrayCall(), two cells are modified instead of one.

Why is the value set to two different cells? How is it possible to only write to one cell?
Thank you in advance for any hints!

My apologies. You are right about the passing by reference

However, your code is accessing memory that does not belong to the array
  String respTable[32][2];An array with 2 dimensions (0 to 31 and 0 to 1)

Later in the code

    Serial.println(respTable[0][0] + " | " + respTable[0][1] + " | " + respTable[0][2]);

An attempt to print the third column that does not exist, so the next area in memory is printed instead

Thanks for that hint. What a mistake from my side - apologies for that, but sometimes you cannot see the wood for the trees. :wink:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.