Auteur Sujet: How to concatenate two chars or strings?  (Lu 10620 fois)

0 Membres et 1 Invité sur ce sujet

Hors ligne faeldaniel

  • Newbie
  • *
  • Messages: 34
    • Voir le profil
How to concatenate two chars or strings?
« le: 26 novembre 2012 à 17:19:49 »
Hello guys,

First, how to work with strings in lib, this is possible?

Second, how do I join two variables of type chars?
Exemple:
char out1  [] = "PVSnes";
char out2  [] = "Lib";

//In java this is possible, but not in C :-(
char out3 []  = out1  + out2 ;

consoleDrawText(1,1, "This is union: %s", out3);

With pointers
char *out1  = "PVSnes";
char *out2  = "Lib";

//This is not possible :-(
char *out3 = out1  + out2 ;

consoleDrawText(1,1, "This is union: %s", out3);

With the console I can do it, but I want to do directly with a variable, exemple:
consoleDrawText(1,13,"Union in console: %s %s", "fael", "daniel");
« Modifié: 26 novembre 2012 à 17:54:37 par faeldaniel »

Hors ligne alekmaul

  • Administrator
  • Hero Member
  • *****
  • Messages: 1149
    • Live Messenger (MSN) - alekmaul@hotmail.fr
    • Voir le profil
    • PortableDev
    • E-mail
Re : How to concatenate two chars or strings?
« Réponse #1 le: 27 novembre 2012 à 05:48:15 »
Hi,
Well, I will try to explain.
Strings are not really flexible in C, you must be sure to have memory space allocated to use them (with 1 more charactere for string termination, the '\0').
Regarding your examples :

Citer
char out1  [] = "PVSnes";
char out2  [] = "Lib";
//In java this is possible, but not in C :-(
char out3 []  = out1  + out2 ;
consoleDrawText(1,1, "This is union: %s", out3);

Must be
Citer
#include <string.h>
char out1[7];
char out2[4];
char out3[10];
strcpy(out1,"PVSnes");
strcpy(out2,"Lib");

strcpy(out3,out1);
strcat(out3,out2);
consoleDrawText(1,1, "This is union: %s", out3);

With pointer, you can't do that, you need to  copy content from one to  another. but as i said, you need to have memory allocated to managed that.

With a variable :
Citer
char out3[11];
sprintf(out3,"%s%s", "fael", "daniel");
consoleDrawText(1,13,"Union in console: %s", out3);

Search with google for string.h, you will see all functions available for C management.

Hors ligne faeldaniel

  • Newbie
  • *
  • Messages: 34
    • Voir le profil
Re : How to concatenate two chars or strings?
« Réponse #2 le: 27 novembre 2012 à 14:54:35 »
Thanks alekmaul for the reply, but the code:
Citer
#include <string.h>
#include <stdio.h>
char out1[7];
char out2[4];
char out3[10];

strcpy(out1,"PVSnes");
strcpy(out2,"Lib");
strcpy(out3,out1);
strcat(out3,out2);

consoleDrawText(1,1, "This is union: %s", out3);
returns the error:
timer.c:66: warning: implicit declaration of function 'strcat'
...
timer.obj:timer.asm:189: FIX_REFERENCES: Reference to an unknown label "strcat".
"make": *** [/d/snesdev/timer/timer.sfc] Error 1
Checked in \devkitsnes\include\string.h but reference strcat not exists, it is necessary to be informed in that path?

Now this code:
char out3[10];
sprintf(out3,"%s%s", "fael", "daniel");
consoleDrawText(1,10,"Union in console: %s", out3)

returns the warning, but continues:
timer.c:61: warning: implicit declaration of function 'sprintf'It Worked perfectly for routines for texts that will develop.
« Modifié: 27 novembre 2012 à 15:17:33 par faeldaniel »

Hors ligne alekmaul

  • Administrator
  • Hero Member
  • *****
  • Messages: 1149
    • Live Messenger (MSN) - alekmaul@hotmail.fr
    • Voir le profil
    • PortableDev
    • E-mail
Re : How to concatenate two chars or strings?
« Réponse #3 le: 27 novembre 2012 à 18:14:59 »
sprintf is in stdio.h.
regarding strcat, you're right. It's not in yet implemented in library. Sorry :/
Need to add it in asm in libc.asm

Hors ligne faeldaniel

  • Newbie
  • *
  • Messages: 34
    • Voir le profil
Re : How to concatenate two chars or strings?
« Réponse #4 le: 28 novembre 2012 à 13:40:28 »
Ok  ;D
Implement in an upcoming release of the lib then, got what I wanted, including the function subString(text, start, length) because it was what I needed, although only work with point, after all the sprintf supports only points.
My code:
char *returnCharsPointMultipleArguments(char *out1, char *out2){

char *outStringPoint;
sprintf(outStringPoint,"%s%s", out1, out2);

return outStringPoint;
 }

char *out1Point = "fael";
char *out2Point = "daniel";

consoleDrawText(1,1, "this is union in return: %s", returnCharsPointMultipleArguments(out1Point, out2Point));

Hors ligne alekmaul

  • Administrator
  • Hero Member
  • *****
  • Messages: 1149
    • Live Messenger (MSN) - alekmaul@hotmail.fr
    • Voir le profil
    • PortableDev
    • E-mail
Re : How to concatenate two chars or strings?
« Réponse #5 le: 29 novembre 2012 à 06:14:44 »
yes, you can use such function waiting for my implementation.
will try to add it soon to lib.