C语言:模拟实现memmove

页面导航:首页 > 软件编程 > C 语言 > C语言:模拟实现memmove

C语言:模拟实现memmove

来源: 作者: 时间:2016-01-25 09:50 【

#define _CRT_SECURE_NO_WARNINGS 1#includestdio.h#includestdlib.h#includeassert.h void *my_memmove(void *p1, void *p2, size_t count){ assert(p1); assert(p2); cha...
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
 
void *my_memmove(void *p1, void *p2, size_t count)
{
    assert(p1);
    assert(p2);
    char *dest = (char *)p1;
    char *src = (char *)p2;
    dest = dest + 16;
    src = src + 8;
    if ((src < dest) && (dest < src + count))
    {
        while (count--)
        {
            *(dest + count) = *(src + count);
        }
    }
    else
    {
        while (count--)
        {
            *dest = *src;
            src++;
            dest++;
        }
    }
    return p2;
}
 
int main()
{
    int arr1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int i = 0;
    int len = sizeof(arr1) / sizeof(arr1[0]);
    int *ret = my_memmove(arr1, arr1, 16);
    for (i = 0; i < len; i++)
    {
        printf("%d ", *(ret + i));
    }
    system("pause");
    return 0;
}

 

Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<