Strcpy vs. memcpy

What is the difference between memcpy() and strcpy() ? I tried to find it using the program, but both give the same output.

 int main() { char s[5]={'s','a','\0','c','h'}; char p[5]; char t[5]; strcpy(p,s); memcpy(t,s,5); printf("sachin p is [%s], t is [%s]",p,t); return 0; } 

Exit

 sachin p is [sa], t is [sa] 
+68
c strcpy memcpy
May 24 '10 at 16:09
source share
8 answers

what can be done to see this effect

Compile and run this code:

 void dump5(char *str); int main() { char s[5]={'s','a','\0','c','h'}; char membuff[5]; char strbuff[5]; memset(membuff, 0, 5); // init both buffers to nulls memset(strbuff, 0, 5); strcpy(strbuff,s); memcpy(membuff,s,5); dump5(membuff); // show what happened dump5(strbuff); return 0; } void dump5(char *str) { char *p = str; for (int n = 0; n < 5; ++n) { printf("%2.2x ", *p); ++p; } printf("\t"); p = str; for (int n = 0; n < 5; ++n) { printf("%c", *p ? *p : ' '); ++p; } printf("\n", str); } 

He will produce this conclusion:

 73 61 00 63 68 sa ch 73 61 00 00 00 sa 

You can see that "ch" was copied to memcpy() , but not strcpy() .

+114
May 24 '10 at 10:14
source share

strcpy stops when the NUL character ( '\0' ) is encountered, memcpy does not. You do not see the effect here, since %s in printf also stops at NUL.

+69
May 24 '10 at 16:11
source share

strcpy exits when the null terminator of the source string is found. memcpy needs to pass a size parameter. In the case when you presented the printf statement, it stops after a null delimiter is found for both arrays of characters, however you will find that t[3] and t[4] also copied the data into them.

+12
May 24 '10 at 16:12
source share

strcpy copies the character from the source to the destination one by one until it finds the NULL character or '\ 0' in the source.

 while((*dst++) = (*src++)); 

where as memcpy copies data (not characters) from the source to the destination of a given size n, regardless of the data in the source.

memcpy should be used if you are well aware that the source contains no character. memcpy is the ideal way for encrypted data or binary data.

strcpy deprecated, so use strncpy .

+9
Dec 05 '12 at 12:36
source share

Due to the null character in your s string, printf will not show anything but this. The difference between p and t will be in characters 4 and 5. p will not have (they will be garbage), and t will have 'c' and 'h' .

+3
May 24 '10 at 16:12
source share

The main difference is that memcpy() always copies the exact number of bytes that you specify; strcpy() other hand, strcpy() will copy until it reads the NUL byte (aka 0), and then stops.

+2
May 24 '10 at 16:11
source share
  • Behavioral difference: strcpy stops when it encounters NULL or '\0'
  • Performance difference: memcpy usually more efficient than strcpy , which always scans the data it copies.
+2
Jan 19 '17 at 18:33
source share

The problem with your test program is that printf() stops inserting the argument into %s when it encounters a null termination of \0 . Thus, in your output, you probably did not notice that memcpy() copies the characters c and h .

I saw in GNU glibc-2.24 that (for x86) strcpy() just calls memcpy(dest, src, strlen(src) + 1) .

0
Feb 16 '18 at 13:18
source share



All Articles