concatenate String

Hi!

I have an error that I can't solve because of my inexperience with language. But, I want to concatenate this String:

String place = "House" ;
const char * user = "user1 /" + place.c_str ();

But I get the following error:

invalid operands of types 'const char [13]' and 'const char*' to binary 'operator+'

How do I concatenate this String into a const char *?

Thanks!

You cannot concatenate 2 c-strings like that. You need to use strcpy() and strcat()

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  char place[] = "House"; 
  const char user[20];   //need room for the full string 
  strcpy(user, "user1 /");
  strcat(user, "House");
  Serial.println(user);
}

void loop()
{
}

Why is place a String in the first place when it could be a c-string

strcpy() and strcat() are fragile methods prone user errors and buffer overruns see Wikipedia Buffer_overflow entry

String are much safer.
try

String place = "House";
String user = String("user1 ") + place;

Actually the + operator has some odd limitations so it is better to do the following

String place = "House";
String user = "user1 ";
user += place;

Or using F(" ") to save RAM

String place = String(F("House"));
String user = String(F("user1 "));
user += place;

Check out my tutorial on Taming Arduino Strings

drmpf:
String are much safer.

That's funny, because my perspective is that in a highly memory constrained environment C-strings are in fact easier to manage in tightly controlled and predictable memory footprints while String objects are much more likely to get away from you with unpredictable memory footprints and catastrophic misbehaviour when memory fills up.

A couple of points

  1. no indication in this post that there is any memory constraint the requires forgoing the inherent safety of Strings
  2. on Uno and Mega2560 Strings are essentially bullet proof in the face of out-of-memory. Check out my link above for the examples. Taming Arduino Strings shows how to control/monitor String memory usage in general.

Finally if do you want to use fixed size char[]s then check out my SafeString library (detailed tutorial here) that wraps up the low level c-string methods with error checks that protect your code from going boom and gives you very clear debug messages, including the name of the offending variable, so you can fix the problems.

As each new micro adds more and more memory, the argument based on memory constraints becomes less and less valid.

drmpf:
As each new micro adds more and more memory

Yeah, some do. But the ATMega328 remains the foundation of 99% of what "Arduino" means, and it is pretty restricted. Personally, I mainly write code for the ATTiny and Strings are next to impossible.

drmpf:
As each new micro adds more and more memory, the argument based on memory constraints becomes less and less valid.

So, more memory means sloppy programming is OK?

If one is using the Arduino to learn C++, then they should simply learn the C++ way to do strings.
If you (or your employer) expects your programs to be readable by others or transportable into other platforms, then they should simply learn the C++ way to do strings.

I mainly write code for the ATTiny

In that case I completely agree, Strings are not suitable. In fact even C has problems, I found I needed to use ASM because C was using extra code saving a lot of registers for each fn call that did not need to be saved.

simply learn the C++ way to do strings

Strings with a capital S, The class C++ added 20 years ago to overcome the systemic coding errors arising from using c-string methods. Microsoft has banned c-string methods and recommends Strings for C++ coding.

Microsoft has banned c-string methods and recommends Strings for C++ coding.

I am willing to bet that Microsoft says no such thing in the context of the Arduino environment which is what is being discussed here

drmpf:
Strings with a capital S, The class C++ added 20 years ago to overcome the systemic coding errors arising from using c-string methods. Microsoft has banned c-string methods and recommends Strings for C++ coding.

When Microsoft comes with advises I choose not to follow them. :grin:

Strings and Arduino is a very bad mix. "Looking for trouble? Use Strings".

"Looking for trouble? Use Strings".

Some examples to support your assertion would be good.
My Taming Arduino Strings shows they are very safe and usable on Arduino.

context of the Arduino environment

The reasons Microsoft bans c-string methods are still relevant. c-string methods are just very fragile and very prone to coder errors.

Thanks for all the tips, I managed to solve using these tips and I understood a little more about the difference in strings. Thanks!

I avoid Strings in all my arduino-code. I used it before since it makes thing easy. My experience is that after a few weeks, the controller start to fail (not stop completely). After I removed the Strings things worked OK.

I learned this by reading postings from other users here, who also warned about using Strings in arduino code.

My experience is that after a few weeks, the controller start to fail (not stop completely).

I would be interested in the String version of you code, if you can make it available. I think Taming Arduino Strings covers all the problems, but would like to check that I have not missed something.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.