Lit Aiy Lit Aiy - 3 months ago 14
C++ Question

Append String object to character array in Arduino

I am using Arduino. I would like to append a String object to an array of characters.

String msg = "ddeeff"

char charArr[1600];

//assume charArr already contains some string
//How can I do something like this to append String to charArray?
charArr = charArr + msg;

Answer

This will work for Arduino String object.

strcat( charArr, msg.c_str() );

String object msg gets converted into an array of characters with the String method c_str(). Then, you can use strcat() to append the 2 arrays of characters.

As mentioned by Rakete1111, it is undefined behavior if charArr is not big enough