[SOLVED] Possible bug in string and char concatenation - Arduino 1.8.10 Linux

Possible bug in string and char concatenation using Arduino 1.8.10 Linux. But if you replace the chars with strings the aoutput is OK.

For instance, you could try the piece of code:

#line 2 "concat.ino"
#include <ArduinoUnit.h>

test(concat){
  byte n = 3;
  char col[n] = {'R','G','B'};
  char tag[n] = {'r','s','t'};
  String strChar = "";
  //String and char concatenation, possible bug
  for (byte i=0; i<n; i++) { 
    strChar += '\n'+col[i]+"= "+tag[i];
  }
  //Serial.println(strChar);

  //String concatenation is OK
  String colStr[n] = {"R","G","B"};
  String tagStr[n] = {"r","s","t"};
  String str = "";
  for (byte i=0; i<n; i++) { 
    str += '\n'+colStr[i]+"= "+tagStr[i];
  }
  //Serial.println(str);

  assertEqual(strChar, str);
}

void setup(){
  Serial.begin(9600);
  while(!Serial) {} // Portability for Leonardo/Micro
}

void loop(){
  Test::run();
}

I get the output:

Assertion failed: (strChar=.) == (str=
R= r
G= s
B= t), file concat.ino, line 24.
Test concat failed
Test summary: 0 passed, 1 failed, 0 skipped, out of 1 test(s).

Best regards.

    strChar += '\n'+col[i]+"= "+tag[i];

I don't think you can use that expression on the right, because none of the right-hand-side values are C++ Strings at permit concatenation using "+"; just chars and C pointers (to char arrays.)

That's it! I love when you post @westfw!

 '\n'+col[i]

The compiler sees that as...

 13 + 82

In C / C++ char is just a very small int. Now the situation makes sense.

I am agreed with the expression '\n'+col[i] is a byte, but I mean the expression strChar += '\n'+col[i]+"= "+tag[i]; is legal and it means strChar = strChar+'\n'+col[i]+"= "+tag[i]; that it works,

eurojet:
is legal and it means

It is legal. You are correct.

That is not what it means. You are incorrect. Operator precedence is relevant.

strChar += '\n'+col[i]+"= "+tag[i];

is legal and it means

 strChar = strChar+'\n'+col[i]+"= "+tag[i];

the entire Right Hand Side is calculated first, and none of it is Strings.

Consider "x *= i + j;"

I think it is a source of errors. In my opinion is not a bad idea a new operator "=+++" that it means: strChar = strChar+'\n'+col[i]+"= "+tag[i]; because you are working with an acumulator of the same type of the leftthand to acumulate and it must consider righthand the entire expression like the above intuitive idea, near the human being language as high level language.

As conclusion for my "Tips & Tricks collection":
x += y; <=> x + ( y); and x =; equivalent to the expression x = x + ( y ); and not x = x + y;

  • In general works as expected: acumulatorString = acumulatorString + char + char* + String +....

  • Operator '+=' only for simple expressions like: String += '\n'; String += char; String += char*; String += byte; String += int; ...

Best regards,

Yeah, string handling in C sucks, and everything associated with it, too.
But you have to be much higher in the language definition hierarchy to change the "standardize language" behavior. They probably have very good reasons for not taking the Left hand side type into account when computing the right hand side, perhaps even more so than "it's always been that way" or "that's the RULE!"

You might have gotten it to work by including a case, something like:

strChar += (String)'\n'+col[i]+"= "+tag[i];