- What is the difference between memcpy and strcpy?
strcpy ends copying of data when it reaches NULL character (Say, '\0' or 0) and then copies NULL at the end of destination data. It is specifically used to copy strings (char[]).
memcpy can be used to copy any type of data (void*). It terminates at the byte position specified by the third parameter.
- #include <stdio.h>
-
#include <string.h>
-
-
void scpy(char *des, char *src)
-
{
-
while (*src)
-
*des++ = *src++;
-
*des = '\0';
-
}
-
-
void mcpy(char *des, char *src, int size)
-
{
-
int i;
-
for (i=0; i < size; i++)
-
des[i] = src[i];
-
}
-
-
int
-
main(void)
-
{
-
char *s = "hello world";
-
char d[12];
-
scpy(d, s);
-
printf("%s\n", d);
-
mcpy(d, s, strlen(s));
-
printf("%s\n", d);
-
-
return (0);
-
}
阅读(1680) | 评论(2) | 转发(0) |