I wonder if there is a way to do something like this:
Your question is about "concatenating" C strings, but I'd like to ask if you really need to concatenate the C strings. It is very likely that you can use the pieces, just like the way they were printed in my previous post. I hope you can just do this:
Serial.print( F("constChar") );
Serial.print( anInt ); // add Information
Serial.print( ',' );
Serial.print( charArray ); // NUL-terminated C string in RAM
Serial.print( '=' );
Serial.println( aFloat );
Serial
isn't the only thing you can print to. You can use the same approach with most other devices/libraries (LCD, SD files, Ethernet, etc.). If you're not sure how to do something, tell us more about what you have and what you're trying to use.
Regarding concatenation: The first "concatenation" is just an assignment or "copy". The next piece is concatenated to the first piece. Here's a snippet from a GPS sketch that shows a few techniques:
char text[60]; // a little bigger than 2+21+6+7+6=42 and 6 commas and a zero byte at the end (49 chars total)
char *ptr = &text[0]; // This is the append point for each piece
// Satellites (2 chars)
itoa( fix.satellites, ptr, 10 ); // write an int at the beginning of text[]
ptr = &ptr[ strlen(ptr) ]; // step to the end of the satellites chars
*ptr++ = ','; // append a comma and step 1
dtostrf( fix.longitude(), 8, 6, ptr ); // write a float
ptr = &ptr[ strlen(ptr) ];
*ptr++ = ',';
The basic idea is to declare an array that's big enough to hold the result, then use the C string functions and pointers to fill the array, stepping along as you put each piece into the array.
Here are the C string functions. Quite a list, no? Some decent summaries of C string (not String
) functions are here and here.
Again, it is usually more efficient to hold onto int and float variables until it's time to print them, one at a time. You may never need a concatenated "message" or the string version of a number.
Cheers,
/dev