Concatenate 5 string at once

Good evening

It seems that my relationship with Arduino, don't have betters...
So, I need your help once again, please.

char StringFinal[40];
char * sHH;
char * sMM;
char * sSS;

sHH="21";
sMM="43";
sSS="00";

//CONCATENATE 5 STRINGS
char *concatena5(char *string1, char *string2, char *string3, char *string4, char *string5) {
StringFinal[0]='\0';
strcat(StringFinal, string1);
strcat(StringFinal, string2);
strcat(StringFinal, string3);
strcat(StringFinal, string4);
strcat(StringFinal, string5);
return StringFinal;
}

void Escreve_LCD2(char *sTexto){
lcd.cursorTo(2,1);
lcd.printIn(sTexto);
}

Escreve_LCD2(concatena5(sHH,":",sMM,":",sSS));

The result that I get is:
":43:00"

What happen to "19" !??? to get"19:43:00"

Thanks on advance
Pedro Ferrer

Good evening

char *concatena5(char *string1, char *string2, char *string3, char *string4, char *string5) {
Serial.println("antes");
Serial.println(string1);
StringFinal[0]='\0';
strcpy(StringFinal, string1);
Serial.println(StringFinal);
Serial.println("depois");
//strcat(StringFinal, string1);
strcat(StringFinal, string2);
strcat(StringFinal, string3);
strcat(StringFinal, string4);
strcat(StringFinal, string5);
return StringFinal;
}

At this point:

Serial.println("antes");
Serial.println(string1);

I have "19" at string1...

and here:

strcpy(StringFinal, string1);
Serial.println(StringFinal);
Serial.println("depois");

I have "" at StringFinal...

What I'm missing!??

Thanks on advance
Best regards

Pedro Ferrer

Done!!

removing:

StringFinal[0]='\0';

the concatenation is well done!

Thanks
Best regards
Pedro Ferrer

The declaration:
StringFinal[0]

There is no storage declared ie a zero size string.
Declare enough storage to cope with all the strings

Can we see the entire sketch? Everything, not just the part that you think is having problems?

I mean strcpy followed by four strcats works just fine as long as the array is large enough. Yon't need to assign '\0' to the first char of the array but it doesn't hurt.

Assigning '\0' to the first char of the array and then doing five strcats works just fine as long as the array is large enough.

You appear to have a global array that is large enough, but since it doesn't work for you, something else must be going on. Really.

Show us everything.

Regards,

Dave

Good afternoon

Unfortunately, it seems that yerterday I missed something... because I still don't get the correct string

char StringFinal[40];
char * sHH;
char * sMM;
char * sSS;

sHH="21";
sMM="43";
sSS="01";

//CONCATENA 5 STRINGS
char *concatena5(char *string1, char *string2, char *string3, char *string4, char *string5) {

strcpy(StringFinal, string1);
strcat(StringFinal, string2);
strcat(StringFinal, string3);
strcat(StringFinal, string4);
strcat(StringFinal, string5);
return(StringFinal);
}

//Function to write on LCD
void Escreve_LCD2(char *sTexto){
lcd.cursorTo(2,1);
lcd.printIn(sTexto)
}

//call function to write the string on LCD
Escreve_LCD2(concatena5(sHH,":",sMM,":",sSS));

When I do:
(...)
Serial.println(StringFinal);
Serial.println(string5);
strcat(StringFinal, string5);
return(StringFinal);
}

I have on serial:
Printing "StringFinal"
21:43:
Printing "string5"
21:43:

I don't know what happen to "01" !!! and the function "concatena5" it seems that return an empty string!!!

I don't get the problem!

I'll appreciate your help once again
Thanks on advance
Best regards

Pedro Ferrer

I have on serial

There is no serial in that sketch.
Why don't you post the sketch you're trying to debug?

Is this:

char StringFinal[40];
char * sHH;
char * sMM;
char * sSS;

sHH="21";
sMM="43";
sSS="01";

legal C?

My code is too complex and extensive to put it here...
I've developed all the aplication on VB, translated to Arduino and I'm debugging now... step by step.

Pleased check the code that follows:~

//CONCATENA 5 STRINGS

char *concatena5(char *string1, char *string2, char *string3, char *string4, char *string5) {

** //If I put here reading "string5", I have on serial "01"**
Serial.println(string5);

strcpy(StringFinal, string1);
strcat(StringFinal, string2);
strcat(StringFinal, string3);
strcat(StringFinal, string4);

** //If I put here reading "string5", I have on serial "21:43:**
Serial.println(string5);

strcat(StringFinal, string5);
return(StringFinal);
}

Why my string5 loose "01" !?? and why "concatena5" blow up? I think that blow up because my LCD show an empty string instead "21:43:" or even "21:43:01"...

Thanks on advance
Best regards

Good afternoon

I've did some checks more...
Please read the text in bold.

char *concatena5(char *string1, char *string2, char *string3, char *string4, char *string5) {

strcpy(StringFinal, string1);
strcat(StringFinal, string2);
strcat(StringFinal, string3);
strcat(StringFinal, string4);

Serial.println("ini");
Serial.println(string5);
strcat(StringFinal, string5);

//### the application blows up here!, because on serial I just have:
ini
21:43:
fim1 and fim2 don't appear on serial

Serial.println("fim1");
Serial.println(string5);
Serial.println("fim2");
return(StringFinal);
}

Thanks on advance
Best regards
Pedro Ferrer

My code is too complex and extensive to put it here...

So, cut it down to the bare essentials necessary to exhibit the problem.
If it works, then your problem lies elsewhere.

"Complex code" and strings not working as expected might point to you running out of memory.

Is your Arduino using a 168 or 328 ATMega?

@AWOL:

legal C?

Yes. Everything he posted was legal C and legal C++ (in bits and pieces). He just didn't post everything. I mean, really!

From the original post:

char StringFinal[40];

void setup()
{
    Serial.begin(9600);
}

//CONCATENATE 5 STRINGS
// Good program design would not use a global variable, but might use a static array
// inside the function or some such thing.  (Maybe pass the array name and size to
// the function and use strncat or some such thing.)
// Actually, I can't imagine that I would ever need a function that
// did nothing more than put five "strings" into an array.
//
// Oh, well...
//
// But as "proof of concept" I'll just use the original, exactly as in
// the first post.
//
// davekw7x
//
char *concatena5(char *string1, char *string2, char *string3, char *string4, char *string5)
{
     StringFinal[0]='\0';
     strcat(StringFinal, string1);  
     strcat(StringFinal, string2);
     strcat(StringFinal, string3);
     strcat(StringFinal, string4);
     strcat(StringFinal, string5);
     return StringFinal;
}

void Escreve_Serial(char *sTexto)
{   
     Serial.println(sTexto);
}
void loop()
{
    char * sHH;
    char * sMM;
    char * sSS;

    sHH="21";
    sMM="43";
    sSS="00";
    Escreve_Serial(concatena5(sHH,":",sMM,":",sSS));
    while (1)
        ;
}

Output:


21:43:00


Regards,

Dave

From reply #5

char StringFinal[40];
char * sHH;
char * sMM;
char * sSS;

sHH="21";
sMM="43";
sSS="01";

Such initialisations outside of a function are not legal.

char * sHH="21";

I'm OK with (and the C compiler is too)

Good evening AWOL

You are right! Please, sorry.
Thanks to your tip, I've found that the problem is when my string is < 10, because I concatenate "0" + sSS string.
If my sSS string is equal or bigger than 10, I don't have that problem and function 'concatena5' works great.
The problem is found... I don't know how to fix it, or avoid...

int intHora;
int intMinutos;
int intSegundos;

char * sHH;
char * sMM;
char * sSS;

const int nHoras=2;
const int nMinutos=1;
const int nSegundos=0;

char Debug[40];
char* cstr(int Parametro){
itoa(Parametro,Debug,10);
return strdup(Debug);
}

char *concatena(char *string1, char *string2) {
StringFinal[0]='\0';
strcat(StringFinal, string1);
strcat (StringFinal, string2);
return StringFinal;
}

char *concatena5(char *string1, char *string2, char *string3, char *string4, char *string5) {
strcpy(StringFinal, string1);
strcat(StringFinal, string2);
strcat(StringFinal, string3);
strcat(StringFinal, string4);
strcat(StringFinal, string5);
return(StringFinal);
}

int LeEEPROM(int ID){
return (EEPROM.read(ID));
}

void GravaEEPROM(int ID, int Valor){
EEPROM.write(ID,Valor);
}

void Escreve_LCD2(char *sTexto){
lcd.cursorTo(2,1);
lcd.printIn(sTexto);
}

void loop() {

GravaEEPROM(nHoras,10);
intHora=LeEEPROM(nHoras);
sHH=cstr(intHora);
if (intHora<10){ sHH=concatena("0",sHH);
}

GravaEEPROM(nMinutos,10);
intMinutos=LeEEPROM(nMinutos);
sMM=cstr(intMinutos);
if (intMinutos<10){
sMM=concatena("0",sMM);
}

GravaEEPROM(nSegundos,1);
intSegundos=LeEEPROM(nSegundos);
sSS=cstr(intSegundos);
if (intSegundos<10){ sSS=concatena("0",sSS);
}

Escreve_LCD2(concatena5(sHH,":",sMM,":",sSS));

delay(5000);
}

I'll appreciate your prompt help
Thanks on advance
Pedro Ferrer

Good morning

I've updated arduino_0018 to arduino_0019 to use String class.

On my project I'm, trying to use the following:

char * sSS;
String stringOne = String(sSS);

Don't work...
Which is the best way to convert char() to string()?

Thanks on advance
Best regards

Pedro Ferrer

Good afternoon

I've found that:

char * sSS;
String stringThree = String(sSS);

works... outside from a Switch...

Inside a switch case, I have the following error:

1>c:\arduino\arduinoIDE\sketches\CODINO_vsAddIn/..\CODINO1.pde: In function 'void GestaoMenus()':
1>c:\arduino\arduinoIDE\sketches\CODINO_vsAddIn/..\CODINO1.pde:685: error: jump to case label
1>c:\arduino\arduinoIDE\sketches\CODINO_vsAddIn/..\CODINO1.pde:652: error: crosses initialization of 'String stringThree'
1>c:\arduino\arduinoIDE\sketches\CODINO_vsAddIn/..\CODINO1.pde:688: error: jump to case label
1>c:\arduino\arduinoIDE\sketches\CODINO_vsAddIn/..\CODINO1.pde:652: error: crosses initialization of 'String stringThree'

Can somebody try to help me? Please.
I can't imagine why don't works inside a switch...

Thanks on advance
Best regards
Pedro Ferrer

You cannot (for some reason) not declare variables inside a switch statement. You should have
String stringThree;
at some place
and
stringThree = String(sSS);
in the switch.

Hello

Thank you Hans!

Build succeeded!
Later at night I will test 5 concatenations.

Thanks on advance
Best regards
Pedro Ferrer

Good afternoon

Unfortunately my 'LCD4Bit_mod' library isn't updated...

error: no matching function for call to 'LCD4Bit_mod::printIn(String&)'

note: candidates are: void LCD4Bit_mod::printIn(char*)

Don't support String yet...

Best regards
Pedro Ferrer