Concatenate char*

Hello!

I need a char* composed by another char* and a String.
char* myFunction(){
String my_str = "sent";
char* chary = "message";
int str_len = my_str.length() + 1;
char buf[str_len];
my_str.toCharArray(buf, str_len);
strcat(chary, buf);
return chary;
}

I tried that but is not working

Thanks!

What is chary? You haven't declared it. Is it large enough to fit the combined strings?

Where do you use charie? You declare it as a constant c string, but you don't use it anywhere.

That code doesn't compile. Please post code that compiles.

Just changed the first post. They were both chary.

Forget Strings and use character arrays. Very straightforward and reliable.

void setup() {
  char buf[30];
  const char *first = "sent ";
  const char *second = "message";
  strcpy(buf,first);
  strcat(buf,second);
  Serial.begin(9600);
  Serial.println(buf);  //prints sent message
}
void loop(){
}
 char* chary = "message";

chary is an array that is EXACTLY large enough to hold 8 characters.

 strcat(chary, buf);

There is NOT enough room to add more characters.

  return chary;

When the function ends, chary goes out of scope. Returning a pointer to memory that can be overwritten at any time is a very bad idea.

When the function ends, chary goes out of scope. Returning a pointer to memory that can be overwritten at any time is a very bad idea.

Paul's right and, even worse, the code may appear to work correctly...for a while.

chary is a pointer to a string literal, which is constant. It's undefined behavior to write to a string literal.

It's undefined behavior to write to a string literal.

It's not undefined at all. The effect may be unexpected, but the behavior (of the compiler) is not undefined.

Chistop: They are not writing to a string literal, they are writing to ca char array that is initialised to a string literal.

Now this would be undefined:
strcat("message", "some more");

PaulS:
It’s not undefined at all. The effect may be unexpected, but the behavior (of the compiler) is not undefined.

It is indeed undefined behavior. This is from the C++ 11 draft standard, section 2.14.5:

The effect of attempting to modify a string literal is undefined.

I don’t have a copy of the older C++ standards, but modifying a string literal has been undefined behavior since the days of C89 (the C++ standards borrow heavily from the C standards). This is from the C89 draft standard, section 3.1.4:

If the program attempts to modify a string literal of either form, the behavior is undefined.

(“either form” means either wide or non-wide.)

You can think of a string literal as being of type “const char *”, which obviously cannot be modified.

KeithRB:
Chistop: They are not writing to a string literal, they are writing to ca char array that is initialised to a string literal.

Nope. This declares chary to be a pointer to char and initializes it to point to a string literal:

char* chary = "message";

You cannot modify the contents of a string literal even through a non-const pointer to it. Besides, the compiler should give you a warning for that like “warning: deprecated conversion from string constant to ‘char*’”, which tells you that you’re probably doing something wrong (g++ version 4.7.4 does for me, even without -Wall).

On the other hand, this would initialize chary to be a string that you can modify:

char chary[] = "message";