Implementation of "C language" memcpy function

Source: Internet
Author: User

1 function prototypes

void *memcpy (void *dest, const void *SRC, size_t n); 2 features copies n bytes from the starting position of the memory address referred to by the source SRC to the beginning of the memory address referred to by the target dest 3 required header files use # include <string.h>; in C languageYou can use # include <cstring> and # include <string.h> in C + +. 4 return value The function returns a pointer to Dest. 5 Description The memory areas referred to by 1.source and Destin may overlap, but if the memory areas referred to by source and Destin overlap, this function does not ensure that the overlapping area of the source is not overwritten before the copy. The use of memmove can be used to process overlapping areas. function returns a pointer to the DestinPointers.2. If the target array Destin itself already has data, after executing memcpy (), the original data will be overwritten (up to N). If you want to append data, add the destination array address to the address you want to append the data to after each execution of memcpy. Note: Both source and Destin are not necessarily arrays, and any readable and writable space is available. its function is implemented as follows:
<span style= "FONT-SIZE:18PX;" > #include <stdio.h> #include <assert.h>void *my_memcopy (void *str1,void const *str2,int len) {char *tmp1= (char*) Str1;char *tmp2= (char*) str2;void *ret=str1;assert (str1);    ASSERT (STR2);   while (len--)   {*tmp1++ = *tmp2++;   }   return ret;} int main () {char q[] = "Abcdjgfjjyty"; char p[] = "DDDDDDD";//bbit-tehmy_memcopy (q,p,6);p rintf ("%s\n", My_memcopy (q,p,6) ); return 0;} </span>

Results:

Implementation of "C language" memcpy function

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.