How to concatenate two chars or strings?

Démarré par faeldaniel, 26 Novembre 2012 à 17:19:49

« précédent - suivant »

0 Membres et 1 Invité sur ce sujet

faeldaniel

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");

alekmaul

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 :

Citationchar 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
Citation
#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 :
Citationchar 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.

faeldaniel

#2
Thanks alekmaul for the reply, but the code:
Citation

#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.

alekmaul

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

faeldaniel

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));

alekmaul

yes, you can use such function waiting for my implementation.
will try to add it soon to lib.