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