Writing memcpy memory copy Function c ++ code example and running result (Code tutorial), memcpy writing

Source: Internet
Author: User

Writing memcpy memory copy Function c ++ code example and running result (Code tutorial), memcpy writing

Memcpy () is a memory copy function. Compared with strcpy, memcpy can only copy strings. memcpy can copy any type of data.

The following code uses c ++ to write a function with the same functions as the memcpy () function.

C ++ code

# Include <iostream> # include <assert. h> using namespace std; void * memCpy (char * dst, char * src, size_t size) {assert (dst! = NULL & src! = NULL); char * Dst = dst; // prevents changes to the dst address char * Src = src; while (size --> 0) * Dst ++ = * Src ++; return dst;} int main () {char src [] = "hello world"; char dst [20]; memCpy (dst, src, 11 ); cout <"Source string is" <src <endl; cout <"Copied string is" <dst <endl; return 0 ;}

Running result

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.