ArduinoCore-API icon indicating copy to clipboard operation
ArduinoCore-API copied to clipboard

String class plus plus

Open frankq opened this issue 12 years ago • 6 comments

String str0, str1, str2, str3;

str0 = str1 + str2 + str3; //// will cause problem str0 += str1 + str2; //// will cause problem

str0 = str1; str0 += str2; str0 += str3; // good.

frankq avatar Mar 15 '13 06:03 frankq

Maybe it get confused and sum the string object address?

lestofante avatar Mar 15 '13 07:03 lestofante

I get a similar problem with integers:

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

void loop() {
  String strA = "A: ", strB = "B: ";
   int num = random(10, 20);

  strA += num + " <- A";
  strB += num;
  strB += " <- B";

  Serial.println(strA);
  Serial.println(strB);
}

It concats strB properly but srtA is weird and corrupted:

B: 15 <- B
A: 
B: 15 <- B
A: ú
B: 18 <- B
A: 
B: 12 <- B
A: ú
B: 17 <- B
A: 
B: 14 <- B
A: B
B: 10 <- B
A: ¥ú

What happened?

JadenTravnik avatar Mar 31 '15 17:03 JadenTravnik

Hi there,

Sorry for late replay. I just got my chips and tried it.

In line below:

strA += num + " <- A";

There is a problem. “num” is declared as an integer, but " <- A" is a string array. When the complier sees this, it takes the address of the first char of the string array as the value ( or you can call it as a string pointer). Now there is another problem, the complier will take the result of “num + “<-A”” as a string pointer, or an integer? I don’t know. By the result of the output, I will believe the result is a string array pointer. So strA += a rondon string pointer will result in an unpredictable result.

In order to give the complier a clear instruction, it should be done like below:

strA += String(num) + " <- A";

Now num will be turned into String, a String plus string array will be consider as a class.

There might be one more problem. += has priority over +, so strA += String(num) will be done first before + “<-A”, this might result in unpredictable result. It should be consider as a mistake, but how does this complier to do it? I don’t know.

If I’d write this program, below will be what I will do:

strA += (String(num) + String(" <- A"));

That will make sure that the complier will make no mistake. or it can be done like strB.

what do you think?

Frank

Calgary, Canada

Sent from Windows Mail

frankq avatar Apr 10 '15 00:04 frankq

I see, thank you for explaining that. I still believe this is an issue though. I don't think that the compiler should just add a number to a string pointer as its both unintuitive and a much less used use-case (I couldn't think of one reason why anyone would want to do that) than just concatenating a number to a string.

JadenTravnik avatar Apr 13 '15 14:04 JadenTravnik

Hi Jaden,

Thanks for your reply. As I know about the C, it’s the first language and it’s very like machine codes.

C is actually a group of functions. So the compiler simply translates C into a group of functions, and that’s also how a compiler works. At the early years when I learned C, as far as I remember an expression like “A += B + 100” would report an error. In an expression like A = B + C + D, it will be translated into A = (B + C) + D, and B + C will be used as parameter for function calling, when the function returns a value X, we get a new expression, A = X + D, so X + D will be new the parameters for the same function calling. Every function has a return value and be put into stack, when CPU returns from a function, it’s pops out the stack and receive the return. So all the functions have the same data type, it’s up to us to define what kind of data it is. The compiler can not decide the right data type for us. There is a default data type in a function calling, that is the first parameter used. So, if we don’t specify the data type, the compiler will use the default, which would always result in unexpected mistakes.

Have fun and wish you the best!

Frank

frankq avatar Apr 13 '15 16:04 frankq

I understand that is the case. It was just annoying coming from languages like java, javascript, C#, etc that perform the concatenation implicitly. Looking further into this I found that other languages like ruby, python, (obviously) C and C++ still need explicit conversion from number to string. In any case, thanks for your help.

JadenTravnik avatar Apr 14 '15 15:04 JadenTravnik