Memcpy () function

Source: Internet
Author: User

1. Implementation of the memcpy () function

Void * memcpy (void * DEST, const void * SRC, size_t N );

Copy n Bytes from the starting position of the memory address in the source SRC to the starting position of the memory address in the target DeST.

 
Void * memcpy (void * DEST, const void * Source, size_t count) {char * ret = (char *) DEST; char * dest_t = ret; // The intermediate variable char * source_t = (char *) source; // The intermediate variable while (count --) {* dest_t ++ = * source_t ++;} return ret ;}

The above Code shows that the destination address and source address remain unchanged after memcpy is executed due to the use of intermediate variables, and the returned Destination Address

 

2. Implementation of strcpy () Functions

Prototype Declaration: extern char * strcpy (char * DEST, const char * SRC );

Header file: # include <string. h>

Function: Copies the string starting from the SRC address and containing the null terminator to the address space starting from DeST.

Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings.

Returns the pointer to DeST.

 
Char * strcpy (char * strdestination, const char * strsource) {assert (strdestination! = NULL & strsource! = NULL); char * strd = strdestination; while (* strd ++ = * strsource ++ )! = '\ 0'); Return strdestination ;}

 

3. Differences between the usage of the memcpy function and strcpy

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

The copy length is determined 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.

If the target array DESTIN already has data, after memcpy () is executed, the original data will be overwritten (N at most ). If you want to append data

After memcpy is executed, add the target array address to the address where you want to append the data.

 

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.