自己实现memcpy函数

来源:互联网 发布:excel纸上数据录入技巧 编辑:程序博客网 时间:2024/06/05 11:03
#include<stdio.h>void* Mymemcpy(void *dest,const void* src,size_t count){    char *tmpDest = (char *)dest;    char *tmpSrc = (char *)src;    size_t i;//内存有覆盖的区域,从尾部开始复制    if((tmpDest > tmpSrc) && (tmpDest < (tmpSrc+count)))    {        for(i = count-1; i != -1; i--)    {        tmpDest[i] = tmpSrc[i];    }    }    else//内存没有覆盖的区域,从头开始复制    {        for(i = 0; i < count; i++)    {        tmpDest[i] = tmpSrc[i];    }    }    return dest;}int main(){    char str[] = "0123456789";    Mymemcpy(str,str+2,4);    printf("%s\n",str);    Mymemcpy(str,str+5,4);    printf("%s\n",str);    return 0;}
原创粉丝点击