The string concatenation example will not compile under the 1.5.3 IDE. It gives an error message about not finding operator+
If you change the example code to use the concat function then all is fine but e.g.
string = string1 + string2
does not compile.
Link Copied
This is a known issue.
It seems like it should be easy to create a function that adds two strings together, but I don't think anyone has taken the time to do it yet.
I have not tested this, but I think this function should work.
String StringAdd(String string1, String string2) {
int len1 = string1.length();
int len2 = string2.length();
char char1[len1];// = string1;
char char2[len2];// = string2;
char ret[len1 + len2];
string1.toCharArray(char1, len1);
string2.toCharArray(char2, len2);
for(int i = 0; i < len1; i++){
ret[i] = char1[i];
}
for(int i = 0; i < len2; i++){
ret[i + len1] = char2[i];
}
String retval = ret;
return retval;
}
There are some bugs in the above code. Attached is a sketch that successfully adds two strings.
String StringAdd(String string1, String string2) {
int len1 = string1.length();
int len2 = string2.length();
char char1[len1];
char char2[len2];
char ret[len1 + len2];
string1.toCharArray(char1, len1 + 1);
string2.toCharArray(char2, len2 + 1);
for(int i = 0; i <= len1 + 1 ; i++){
ret[i] = char1[i];
}
for(int i = 0; i <= len2 + 1; i++){
ret[i + len1] = char2[i];
}
String retval = ret;
return retval;
}
As I pointed out in the original post the String.concat function works and I had already corrected the example using that. It seems much simpler than using the function posted above.
So this currently will not compile:
s3 = s1 + s2;
but this does:
s3 = s1;
s3.concat(s2);
and works fine.
Is there a bug tracking service for the galileo IDE? I would have checked to see if it was known but can't find it.
This string add operator problem was associated to Galileo firmware version 0x0000030e / 782. You can find the firmware version by using cat /sys/firmware/board_data/flash_version from the Galileo Linux command prompt.
This issues appears to have been corrected in Galileo firmware 0x01000000 / 1.0.0. Just grab the latest version of the IDE with the 1.0.0 FW build from /docs/DOC-22226 https://communities.intel.com/docs/DOC-22226 and update the Galileo Firmware (Ardinuo IDE -> "Help" -> "Firmware Update").
--Matt Royer
For more complete information about compiler optimizations, see our Optimization Notice.