strcpy and memcpy are standard C library functions that have the following characteristics.
strcpy provides a copy of the string. That is, strcpy is used only for string copying, and it also copies the end character of the string, not just the string content.
The prototype of the known strcpy function is: char* strcpy (char* dest, const char* SRC);
MEMCPY provides a common memory copy. That is, memcpy has no limitations on what needs to be replicated, so it is more versatile.
void *memcpy (void *dest, const void *SRC, size_t count);
Copy Code code as follows:
char * strcpy (char * dest, const char * src)//implementation of SRC to dest replication
{
if (src = NULL) | | (dest = NULL)) Determine the validity of parametric SRC and dest
{
return NULL;
}
char *strdest = dest; Save the first address of the target string
while ((*strdest++ = *strsrc++)!= ' "); Copy the contents of the SRC string to dest
return strdest;
}
void *memcpy (void *memto, const void *memfrom, size_t size)
{
if ((Memto = NULL) | | (Memfrom = NULL)) Memto and Memfrom must be effective
return NULL;
Char *tempfrom = (char *) memfrom; Save Memfrom First Address
Char *tempto = (char *) memto; Save Memto First Address
while (Size-> 0)//cycle size, copy memfrom value into Memto
*tempto++ = *tempfrom++;
return memto;
}
strcpy and memcpy mainly have the following 3 aspects of the difference.
1, the content of replication is different. strcpy can only copy strings, and memcpy may copy arbitrary content, such as character arrays, integers, structs, classes, and so on.
2, the method of replication is different. strcpy does not need to specify a length, it encounters the string end of the copied character "" "only to end, so it is easy to overflow. memcpy determines the length of the copy based on its 3rd parameter.
3, the use of different. You typically use strcpy when you copy strings, and you need to copy other types of data in general memcpy