Go Down

Topic: [SOLVED] Possible bug in string and char concatenation - Arduino 1.8.10 Linux (Read 1 time) previous topic - next topic

eurojet

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:

Code: [Select]

#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.

westfw

Code: [Select]
    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.)

Coding Badly


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

Code: [Select]
'\n'+col[i]

The compiler sees that as...

Code: [Select]
13 + 82

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


eurojet

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

Coding Badly

is legal and it means
It is legal.  You are correct.

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


westfw

Quote
Code: [Select]
strChar += '\n'+col[i]+"= "+tag[i];

is legal and it means
Code: [Select]
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;"

eurojet

I think it is a source of errors. In my opinion is not a bad idea a new operator "=+++" that it means:
Code: [Select]
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,

westfw

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:
Code: [Select]
strChar += (String)'\n'+col[i]+"= "+tag[i];



Go Up