bug?

Am I just spacing out or is this a bug? You can't add two char arrays to make a string whereas you can add a String and a char array.

void setup() {
  Serial.begin(9600);
  char temp1[] = "test ";
  char temp2[] = "one";
  String temp3 = "two";
  String stest = "empty";
  //stest = temp1 + temp2;  // fail
  //stest = "test " + temp2;  // fail
  //stest = "foo" + "bar";  // fail
  stest = temp1;
  stest = temp2 + temp3;
}

character arrays and the String object are completely different.

There is a method in the Arduino library for handling concatenating String objects (which historically was broken). No such method exists for character arrays.

This compiles:

void setup() {
  Serial.begin(9600);
  char temp1[] = "test ";
  char temp2[] = "one";
  String temp3 = "two";
  String stest = "empty";
  stest = String (temp1) + temp2; 
  stest = String ("test ") + temp2;
  stest = String ("foo") + "bar"; 
  stest = temp1;
  stest = temp2 + temp3;
}

void loop () {}

The String class supports concatenation via the "+" operator. For character arrays, doing an addition just adds the addresses (the pointers).

Also see this: http://www.gammon.com.au/concat

But Nick, This...

stest = temp2 + temp3;

works where temp2 is a char array and temp3 is a string. If it only used the address of the char array it should be something like 12345two but it prints "onetwo"

Once you promote temp2 to String the concatenation accepts a const char* as an operand.

See here from the .h file for String:

    String & operator += (const String &rhs)    {concat(rhs); return (*this);}
    String & operator += (const char *cstr)     {concat(cstr); return (*this);}

Once you have a String on the left, it accepts both String on the right and "const char *".

I see you are saying something a bit different. It looks like they fudge that with StringSumHelper:

    friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
    friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);

...

class StringSumHelper : public String
{
public:
    StringSumHelper(const String &s) : String(s) {}
    StringSumHelper(const char *p) : String(p) {}

It looks like the initial string gets promoted to StringSumHelper and then that gets added to a String, giving the results you noted.

Your article looks very interesting I think it will be quite useful when I digest it.

I do think this is a bug.

char chr[] = "foo"; char chr2[] = "fu"; String str = "bar"; newString = chr + str; ---- works newString = str + chr; ---- works newString = str + str; ---- works newString = chr + chr2; ---- fails - won't compile

Perhaps you should have to cast the char array to a String. newString = String(chr) + str;

Ok, I guess you talked me out of using String. It's just that I've always hated having these buffers sitting around wasting space when not being used. Unfortunately it looks like String wastes way more memory and time than those buffers ever will.

The state machine is by far the coolest approach but it wants some very specific kinds of input and I want something more open ended.

it wants some very specific kinds of input and I want something more open ended

What is that you are doing exactly ?

TheNorm: Your article looks very interesting I think it will be quite useful when I digest it.

I do think this is a bug.

char chr[] = "foo"; char chr2[] = "fu"; ... newString = chr + chr2; ---- fails - won't compile

Perhaps you should have to cast the char array to a String. newString = String(chr) + str;

It's not a bug. You cannot add two pointers and assign the resulting address to a String.

you can concatenate (add) two char array's by means of the strcat() function, see - http://www.tutorialspoint.com/c_standard_library/c_function_strcat.htm -

There are some restrictions like there must be enough memory allocated ....