Problems with strcat

Hi again!
Now that you helped me so much, the program is at least free from syntax errors.
There seems to be one little (maybe logical?) error in it.
I have modified the program so that everyone could test it within the Arduino sketcher in the serial monitor.

My aim is to merge three (or more) string and/ or int variables.
First, I make a String out of any int with "makeString".
After that, I would like to join these into one string.
It seems, that the "makedString" does cause problems, because if you call it via

char* lcdOutput = join3Strings("Mode ", "1" , " selected");

instead of

char* lcdOutput = join3Strings("Mode ", stringToBeJoined , " selected");

it does perfectly work!
Any ideas??

I hope, you can help me again :slight_smile:
Thanks a lot!

Sebastian

char makedString[20];
int userSelectionMode = 1;

void setup() {
  char* stringToBeJoined = makeString(userSelectionMode);
  char* lcdOutput = join3Strings("Mode ",stringToBeJoined , " selected");
//  This does work:
//  char* lcdOutput = join3Strings("Mode ", "1" , " selected");  

// DEBUG
Serial.begin(9600);
Serial.print(lcdOutput);
}

// Subroutine: makeString
char* makeString(int zahl) {
  itoa(zahl,makedString,10);
  return makedString;
} 

// Subroutine: join2Strings
char* join2Strings(char string1[40], char string2[40]) {
  strcat (string1, string2);
  return string1;
} 

// Subroutine: join3Strings
char* join3Strings(char* string1, char* string2, char* string3) {

  // This does not work properly. It does only show "Mode 1", but NOT "Mode 1 selected", as it should!!
  strcat (string1, string2);
  strcat (string1, string3);
  
// When putting the strings together in the wrong order, at least ALL strings are displayed:
//  strcat (string1, string3);
//  strcat (string1, string2);
  
  return string1;
}

void loop(){
}

No one any idea...? :frowning:

For strcat to work, you have to have enough space in the first string to add the other strings, which won't happen if you pass it a quoted string produced by the compiler. Try something like this:

char bigstring[40];  // enough room for all strings together

char* join3Strings(char* string1, char* string2, char* string3) {
  bigstring[0] = 0;          // start with a null string:
  strcat(bigstring, string1);   // add first string
  strcat (bigstring, string2);
  strcat (bigstring, string3);

  return bigstring;
}

Sounds logical...
I will try it as soon as I am on my home computer again.
Thanks so far!

PERFECT!! Is does work now! AWESOME!
Thanks a lot!