strcpy, mencpy, memmove three differences

Source: Internet
Author: User

First look at strcpy, the goal is to achieve the copying of strings, here are a few points to note:

1. Judging the validity of pointers

2. Return the copied pointer address, in order to support chained operation

3. Don't forget to copy the last ego of the string to Dest

4. Note that the original string type is const and is guaranteed not to be modified inside the function

Char* mystrcpy (charconstchar* src) {    assert (dest!=null && src! =NULL)    ; Char* r = dest;      while ((*dest++ = *src++)! ='+ ')    ; return R;}

Then look at memcpy, which is designed to replicate memory, which is not limited to string copying and specifies the size of the replication memory

void* mymemcpy (voidconstvoid* src, size_t N) {    assert (dest!=null && src! =NULL)    ; void* r = dest;      while (n--) {        *dest++ = *src++;    }     return R;}

Finally, Memmove, the realization of the function, and the mencpy is the same, the difference is that it deals with the memory of the object and the original memory overlap situation.

void* Mymemmove (void* Dest,Const void*src, size_t N) {Assert (dest!=null&& src!=NULL); void* r =dest; if(Dest<src | | dest>str+n) {//No recover         while(n--){            *dest++ = *src++; }    }Else{//with recover, copy from end to beginDest = dest+N; SRC= src +N;  while(n--){            * (--dest) = * (--src); }    }    returnR;}

In general, strcpy, memcpy, and Memmove are all memory-copying operations, except that strcpy emphasizes the handling of strings, memcpy the size of the copied content, and Memmove is aware of memory overlap

strcpy, mencpy, memmove three differences

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.