I'm not good in C, top of that I'm doing after very long, I need to do a very simple thing:
char code[]="aasd";
char *rmessage="";
strcat(rmessage,code[0]);
I simply want to concatenate the content of index 0 of array code
to rmessage
.
I'm not good in C, top of that I'm doing after very long, I need to do a very simple thing:
char code[]="aasd";
char *rmessage="";
strcat(rmessage,code[0]);
I simply want to concatenate the content of index 0 of array code
to rmessage
.
You need to ensure there is enough space in rmessage
to store the result of the concatentation. You can use strncat to specify the number of characters to copy from a string:
char code[] = "aasd";
char rmessage[1024] = "";
strncat(rmessage, code, 1);
or, in this case, just assign the first character of rmessage
:
rmessage[0] = code[0];
Not coding in C for long time.I think the syntax is just correct.
int sz=10; // sz = # number of chars you want to store + 1 , i assumed 9 characters will be stored at max.
char code[] = "aasd";
char *rmessage = malloc(sz*sizeof(char));
rmessage[0]=code[0];
rmessage[1]=NULL;
*Remember to deallocate the memory allocated to rmessage after your job is done.
free(rmessage);