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.