1

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.

Luca Martini
  • 1,434
  • 1
  • 15
  • 35
Noor
  • 19,638
  • 38
  • 136
  • 254
  • The two declarations are not the same. This [link](http://stackoverflow.com/questions/3862842/difference-between-char-str-string-and-char-str-string) may help you. – Sunmit Girme Mar 21 '12 at 09:03

2 Answers2

4

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];
hmjd
  • 120,187
  • 20
  • 207
  • 252
1

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);
Azad Salam
  • 146
  • 7