Archived

This topic is now archived and is closed to further replies.

johnnyBravo

how to copy 2 char arrays using a method?

Recommended Posts

johnnyBravo    100
Hi, are there any methods to copy to char arrays together eg char buf1[256]; char buf2[256]; i tried doing strcpy(buf1,buf2); but it doesn''t seem to work any ideas?

Share this post


Link to post
Share on other sites
Chris Hare    462
What error is given?


char buf1[256] = "hello, baby!";
char buf2[256];

strcpy(buf2, buf1);

cout << buf1 << endl << buf2 << endl;


should work.


blah blah blah.

Share this post


Link to post
Share on other sites
quote:
Original post by johnnyBravo
Hi, are there any methods to copy [two] char arrays together

What do you mean by "together"? strcpy replaces one char array with a null terminated string from the other. strcat takes one string and copies it to the null terminating location of the other string. (ie buf1 = "foo" buf2 = "bar" then strcat(buf1, buf2) = "foobar"). If the char array isn''t a string then you''ll want to just use memcpy(buf1, buf2, 256) to copy the entirety of one array to the other.



Erzengel des Lichtes
Archangel of Light

Share this post


Link to post
Share on other sites