自己实现memcpy
2024-01-04 16:45 钟铧若岩 阅读(20) 评论(0) 编辑 收藏 举报//memcpy()函数的模拟实现 #include<assert.h> #include<stdio.h> void* my_memcpy(void* destination, const void* source, size_t num) { assert(destination); //断言防止函数接收空指针进行操作 assert(source); void* ret = destination; while (num--) { *(char*)destination = *(char*)source; destination = (char*)destination + 1; source = (char*)source + 1; } return ret; } int main() { int arr1[] = { 1,2,3,4,5,6,7,8,9,10 }; int arr2[10] = { 0 }; //使用my_memcpy()函数将arr1数组的前20字节(即前5个整形)拷进arr2中 my_memcpy(arr2, arr1, 20); for (int i = 0; i < 10; i++) printf("%d\n", arr2[i]); return 0; }