Difference between strcpy and memcpy (reprinted)

Source: Internet
Author: User

Strcpy and memcpy are both standard C library functions, which have the following features.
Strcpy provides string replication. That is, strcpy is only used for string copying. It not only copies the content of the string, but also copies the end character of the string.

It is known that the prototype of the strcpy function is char * strcpy (char * DEST, const char * SRC );
Memcpy provides general memory replication. That is, memcpy has no restrictions on the content to be copied, so it is widely used.
Void * Memcpy ( Void *Dest, Const Void *SRC, Size_t Count );

char * strcpy(char * dest, const char * src) // Implement the replication from SRC to dest{  if ((src == NULL) || (dest == NULL)) // Determine the validity of the SRC and DEST Parameters  {       return NULL;  }  char *strdest = dest;        // Save the first address of the target string  while ((*strDest++ = *strSrc++)!=‘\0‘); // 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 valid         return NULL;  char *tempFrom = (char *)memFrom;             // Save the first memfrom address  char *tempTo = (char *)memTo;                  // Save the first memto address  while(size -- > 0)                // Loop size times, copy the value of memfrom to memto         *tempTo++ = *tempFrom++ ;    return memTo;}

Strcpy and memcpy have the following three differences.
1. The copied content is different. Strcpy can only copy strings, while memcpy can copy any content, such as character arrays, integers, struct, and classes.
2. The replication method is different. Strcpy does not need to specify the length. It ends with the string Terminator "\ 0" of the copied character, so it is prone to overflow. Memcpy decides the copy Length Based on its 3rd parameters.
3. Different purposes. Strcpy is usually used to copy strings, while memcpy is generally used to copy data of other types.

 

Reprinted from http://www.cnblogs.com/stoneJin/archive/2011/09/16/2179248.html

Difference between strcpy and memcpy (reprinted)

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.