Short answer: use std::string
to create the concatenated string.
std::string s = ((std::string(name) + ": ") + data) + ",";
_serial->println( s.c_str() );
Long answer: when concatenating C-style strings, you need a destination buffer that's large enough to hold the result. If you know the maximum size that the result can ever get to, you can declare a local array of that size, and use sprintf
as the other answers explains.
Or, if you don't know the size in advance, you can use a combination of strlen
and new[]
to allocate a dynamically size buffer, do the printing, then delete[]
the buffer. But don't do this! Use a string class instead, either std::string
, or as Drew Dormann mentions in the comments below, an Arduino specific String
class.