模拟实现内存函数memcpy
针对内存块,不在乎内存中的数据。
拷贝内容有重叠的话应用memmove
模拟实现:
代码:
模拟实现memcpy
#include<stdio.h>
#include<assert.h>
void* my_memcpy(void* dest, const void* src, size_t num) {assert(dest != NULL);assert(src != NULL);void* ret = dest;while (num--) {*(char*)dest = *(char*)src;dest = (char*)dest+1;src = (char*)src+1;/*++(char*)dest;//存在缺陷,不分情况下无法完成编译++(char*)src;*/}return ret;
}
int main() {/*char arr1[20] = "xxxxxxxxxx";char arr2[] = "abnbc";*/int arr1[10] = { 0 };int arr2[] = { 1,2,3,4,5,6,7,8 };my_memcpy(arr1, arr2, 20);int i = 0;for (i = 0; i < 10; i++) {printf("%d ", arr1[i]);}/*printf("%s\n", arr1);*/return 0;
}