C memmove函数代码实现

void * Mymemcpy(void * dest ,const void *src ,size_t count)
{
    void *temp = dest;
    if (dest< src)
    {
        while (count--)
        {
            *(char *)dest = *(char *)src;
            ++(char *)src;
            ++(char *)dest;
        }
    }
    else if(dest > src)
    {
        (char *)dest += count;
        (char *)src += count;
        count++;
        while (count--)
        {
            *(char *)dest = *(char *)src;
            --(char *)src;
            --(char *)dest;
        }
    }

    return temp;
}

 或者简便写

void * Mymemmove(void * dest ,const void *src ,size_t count)
{
    void *temp = dest;
    if (dest< src)
    {
        while (count--)
        {
            *(char *)dest = *(char *)src;
            ++(char *)src;
            ++(char *)dest;
        }
    }
    else
    {
        while (count--)
        {
            *((char *)dest+ count) = *((char *)src + count);
        }
    }

    return temp;
}

 

posted @ 2021-04-13 07:40  紅人  阅读(274)  评论(0编辑  收藏  举报