The difference between strcpy and memcpy

Source: Internet
Author: User

When copying a string, I usually use the strcpy or strncpy function, of course, the memcpy function can also be implemented. So what's the difference between strcpy and memcpy?

Here is a piece of information I found (original address: http://www.cnblogs.com/stoneJin/archive/2011/09/16/2179248.html)

The difference between strcpy and memcpy

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.

Char *strcpy (char * dest, const char * src)//copy of SRC to dest
{
if (src = NULL) | | (dest = NULL))  To determine the validity of parameter src and dest
{return
 
NULL;
}
Char *strdest = dest;        Saves the first address of the target string while
((*strdest++ = *strsrc++)!= ' ");//Copy the contents of the SRC string to the dest under return
strdest;
}
void *memcpy (void *memto,const void *memfrom,size_t size)
{
if (Memto = = NULL) | | (Memfrom = NULL))  Memto and Memfrom must be valid 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 to memto
       *tempto++ = *tempfrom++;  
return memto;
}

strcpy and memcpy mainly have the following 3 differences.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.