编写函数,模拟实现memcpy
程序代码如下:
#include <stdio.h>
#include <assert.h>
void *MyMemcpy(void *dest, const void *src, size_t count)
{
assert(dest);
assert(src);
void *ret = dest;
while (count--)
{
*(char *)dest = *(char *)src;
dest = (char *)dest+1;
src = (char *)src+1;
}
return ret;
}
int main()
{
int arr1[10] = {1,2,3,4,5,6,7,8,9,0};
int arr2[10] = {5,4,3,2,1};
int i = 0;
void *r = MyMemcpy(arr1, arr2, 5 * sizeof(int));
for (i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
return 0;
}
程序运行结果如下: