C analog function memcpy

1. Topic

implement memcpy

2. Program code

The function of the memcpy function is to copy n bytes from the starting position of the memory address pointed to by the source to the starting position of the memory address pointed to by the target t

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <Windows.h>
#include <assert.h>

void* my_memcpy(void *obj, const void *ori, size_t count)
{
    void *ret = obj;//创建一个临时变量来存放obj的首地址

    while (count--)//执行count次循环
    {
        *(char *)obj = *(char *)ori;
        ++(char *)obj;
        ++(char *)ori;
    }

    return ret;
}

int main()
{
    int arr1[10] = { 0 };
    int arr2[] = { 1, 2, 3, 4, 5, 6, 7, 8};
    int i = 0;

    my_memcpy(arr1, arr2, 32);

    for (i = 0; i < 10; i++)
    {
        printf("%d ", arr1[i]);
    }

    system("pause");
    return 0;
}

3. Execution results

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325871318&siteId=291194637