Difference between strcpy and memcpy, and between strcpymemcpy

Source: Internet
Author: User

Difference between strcpy and memcpy, and between strcpymemcpy

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 string content, but also copies the string Terminator. strcpy_s is safer!

It is known that the prototype of the strcpy function is char * strcpy (char * dest, const char * src );

The returned value is char * for chained expression.

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;}

Strcpy_s is a secure version of strcpy. It is secure because it performs out-of-bounds checks when copying strings. The following is the implementation code of strcpy_s, which can be found in the tcscpy_s.inl file:
Reference http://www.cnblogs.com/chenkunyun/archive/2012/03/20/2408365.html

errno_t __cdecl _FUNC_NAME(_CHAR *_DEST,size_t _SIZE,const _CHAR *_SRC){     _CHAR *p;    size_tavailable;       /* validation section */    _ VALIDATE_STRING (_ DEST, _ SIZE); // VALIDATE_STRING should be used to verify the validity of the string and determine whether to end with null.    _ VALIDATE_POINTER_RESET_STRING (_ SRC, _ DEST, _ SIZE); // _ VALIDATE_POINTER_RESET_STRING should be the original information of the record string, so that it can be restored after the copy fails.      p = _DEST;    available = _SIZE;    while((*p++ = *_SRC++) != 0 && --available > 0)     {    }      if(available == 0)     {        _RESET_STRING(_DEST, _SIZE);//        _ RETURN_BUFFER_TOO_SMALL (_ DEST, _ SIZE); // when the destination space is insufficient, the string is restored based on the information recorded by _ VALIDATE_POINTER_RESET_STRING and (in Debug mode) report errors in the pop-up dialog box.        _FILL_STRING(_DEST, _SIZE, _SIZE - available + 1);    _RETURN_NO_ERROR;}

_ FILL_STRING completes the work of adding a null terminator to the end of the string.

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.


In C, the strcpy function is different from the strcat function.

C language functions
String processing strcpy strcat function usage: 1) strcat is used to connect two strings. The prototype is char * strcat (char * dest, char * src ), add the src string to the end of dest (overwrite '\ 0' at the end of dest) and' \ 0' 2) strcpy is used to copy the string to the specified place. The prototype is char * strcpy (char * dest, const char * src ), the function is to copy the string starting from the src address and containing the NULL terminator to the address space starting from the dest. Note that strcat is operated from the end of the dest, strcpy directly overwrites the content directed to by dest. 3) in C language, the header file is <stringh>.

What is the difference between the stpcpy function and the strcpy function?

No difference. stpcpy is not a library function and is used on some unix systems.

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.