In my current project I append variables and constant strings to create reports during program execution. After receiving gibberish and even creating runtime crashes I probed a little deeper and found out that a = a + b; isn't the same as a += b; when it comes to strings.
Here the code for a short program which illustrates what I'm talking about:
Hi
you are correct, += with numeric variables, it does a sum, but with String type variables, it concatenates the values.
Try your code by transforming your numeric variables into Strings.
So:
for the quick response. I apologize: It was my first post and I should've remarked that I don't look for a problem solution (I replaced a += when used with array elements by a = a + and the problem went away).
This was rather questioning the validity of the implementation of the += operator in the Arduino compiler. As I see it within the for-loop myTable[k] should point to consecutive integer numbers in the array (i.e. 10, 9, 8, ..). And it does that as expected for report1, 2 and 3. However in report4 it seems to point somewhere else in memory (in the example to the end of the fixed string "result: ") using the index k as a sort of "length of string" operator to the left of the pointer (i.e. "", " ", ": ", "t: ", "lt: " ..). That's not what one should expect or is it?
Don't you agree it would be worthwhile for someone who understands the compiler to review how the += operator is interpreted (and maybe for -=, *= and /= too) when used with array elements?
It computes the right side first.
Mytable[k] is an int. “, “ is a C string. Adding those together does not give you what you want or expect, but it’s correct C.
If you are using an AVR based MCU (like Uno, Nano, MEGA etc.), you should not use the "String" type / class at all since it will cause memory fragmentation witch eventually may lead to malfunction of the device.
When you do String += int, the right side will be added as if it was a char, and char = 1 is not the same as char = '1' . When you do String = String + int then the right side will be added as String because that is the first data type found and thus the following int will be correctly converted to a numeric string. This behavior is one of the dangers of using the String thing.
Thanks for your interest in this matter. I agree that the += operator is defined for numbers in the Arduino Reference:
x += y; and y: variable or constant. Allowed data types: int, float, double, byte, short, long
However the same Reference on String Appending Operators says:
" . . use the += operator and the concat() method to append things to Strings. "
It doesn't limit the use to strings only. And as my example shows the operator works correctly for report2 (I'm using it several times with mixed data types in my program w/o problems). And I believe it also works for report4. It's rather the indexing of the array which goes haywire and produces the unexpected results.
So: I know how to avoid this problem but shouldn't someone take a look at either the Arduino compiler and/or the Arduino Reference if either may benefit from an update?
The results you get are correct, it is not a compiler error. The problem with report4 is that the right side has two data types where one is int and the other is char*. You are mixing a number with a pointer and the two will be added as numeric values and then added to left side as char*, so your code does something like:
char * comma = ", ";
String str = "report:";
for (int i = 0; i < 10; i++) str += (char*)(i + (int)comma);
You are in other words trying to address something that is not allocated. As a proof, you could run this code:
char *gotcha = "1 2 3 4 5 6 7 8 9 10";
char *comma = ", ";
String str = "report:";
int ma[10];
for (int i = 0; i < 10; i++) ma[i] = 10 - i;
for (int i = 0; i < 10; i++) str += ma[i] + comma;
Serial.println(gotcha);
Serial.println(str);
I understand what you're saying but even admitting that I'm not a good programmer however I don't see why following the Arduino Reference should be "piss poor coding" as you termed it.
The Reference states: x += y; // equivalent to the expression x = x + y;
but apparently report = report + String(myTable[k]) + ", "; is not the same as report += String(myTable[k]) + ", "; .
Anyway: I was only trying to help by pointing out something that may be misleading to newcomers. I consider the point made and the subject thus closed.
Not so fast. If I understand the problem, or not-problem, and the explanation, which I can cheerfully allow I may not, it seems a simpler example of the difference between the two forms could be made using just integers of different lengths (promotion, sign extension) or ints and floating point numbers (implicit conversations).
When I can I'll try, but maybe one of the heavies would be able to quickly show that we can get screwed in trouble with relatively innocent expressions what ought to work perfectly as one might have mistakenly expected to be the case with Strings.