4
Contributors
10
Replies
12
Views
4 Years
Discussion Span
Last Post by Ancient Dragon
0

strcpy() uses '\0' as the stopping character while memcpy() requires you to specify how many bytes are being copied. memcpy() isn't guaranteed to be safe if the source and destination blocks overlap while memmove() does have that guarantee at the cost of performance.

Due to the types involved, the mem* functions are also more generic than the str* functions.

0

Quoted Text Here

strcpy() uses '\0' as the stopping character

char str[] = "mem\0abc";
char str1[20];
memcpy (str1,str,10);
//strcpy(str1,str);
puts (str1);

both memcpy and strcpy giving me same output i.e. mem

0

strcpy() and memcpy() aren't the ones doing the outputing. Obviously if there's a null character in the middle of the string, puts() will stop on it.

0

this means when i used memcpy the whole string is copied in str1 while using strcpy str1 contains only "mem"??

0

this means when i used memcpy the whole string is copied in str1 while using strcpy str1 contains only "mem"??

Yes, though since you told memcpy() to copy 10 bytes, you wrote too many characters and invoked undefined behavior. str only contains 8 characters, after all (including null characters).

0

and what about memcpy and memmov?
can u explain me with the help of a code?

Edited by shanki himanshu

0

str1="strings are good");
memmove(str1+8,str1+11,4);
returns------ strings are are good
memcpy(str+8,str1+11,4)
returns------ strings are read

0

str1="strings are good");
memmove(str1+8,str1+11,4);
returns------ strings are are good
memcpy(str+8,str1+11,4)
returns------ strings are read

i run the above code.
i am getting "strings googood" as my output for both cases
i understand my memmove output but why it is same as memcpy?

Edited by shanki himanshu

0

i understand my memmove output but why it is same as memcpy?

Both functions produce the same results. The only real difference is whether the destination and source buffers overlap. If they don't, then use memcpy(), otherwise if they do then use memmove().

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.