When "memcpy" should be used over "strcpy" and viceversa?
Thanks
strcpy is used to copy null terminated strings, e.g.
-
char *strcpy(char *destination, const char *source);
-
copy string source to string destination stopping after the terminating null-character is copied; return a pointer to destination,e.g.
-
char ch1[10]="hello";
-
char ch2[10];
-
strcpy(ch2, ch1);
-
"hello" + terminating null is copied from ch1[] to ch2[]
memcpy() is an efficent way to copy memory areas
-
void *memcpy(void *destination, const void *source, size_t number);
-
copy at most number bytes from source to destination; return a pointer to destination
you can use it as a general tool to copy areas of memory, e.g.
-
int a[10] = {1,2,3,4,5,6,7,8,9,10};
-
int b[10]={0};
-
memcpy(b, a, sizeof(int)* 10);
-
10 ints are copied from a[] to b[]
for discussion on efficency of memcpy see
http://www.embedded.com/showArticle.jhtml?articleID=19205567