首页 > 代码库 > strcpy实现

strcpy实现

#include <bits/stdc++.h>#include <iostream>using namespace std; void* my_memcpy(void* dest,const void* src,unsigned int count){    if (dest == NULL || src =http://www.mamicode.com/= NULL)    {        return NULL;    }     void * ret = dest;    if (dest <= src || ((char*) dest) >= ((char*)src + count))        // 低地址到高地址     {        while (count--)        {            *(char*)dest = *(char*)src;            dest = (char*)dest + 1;            src = (char*)src + 1;        }    }    else                                                             //高地址向低地址     {        dest = (char*) dest + count -1;        src  = (char*) src + count -1;        while (count --)        {            *(char*)dest = *(char*)src;            dest = (char*)dest - 1;            src = (char*) src - 1;        }    }    return ret;}char* my_strcpy(char* dest,const char* src){    if (dest == NULL || src =http://www.mamicode.com/= NULL)    {        return NULL;    }    char* ret = dest;    //没有考虑内存重叠情况     //while ((*dest++ = *src++) != ‘\0‘);    my_memcpy(dest,src,strlen(src)+1);    return ret;}int main(){    char str[10] = "abc";    //my_strcpy(str+1,str);    memcpy(str+1,str,sizeof(str));     cout << str << endl;}

 

strcpy实现