代码
- 一开始保存被更改的数组地址,因为后面指针会移动,新定义一个指针用来指向首元素地址
- 不知道指针str1和str2的步长,不妨强制转换成cha*指针,让两个指针每次只走一个字节
- numb存放的是需要拷贝过去的数据字节长度
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
//模拟实现内存拷贝函数
void* myMemcpy(void* str1, void const* str2, size_t numb) {
void* ret = str1;
while (numb--) {
*(char*)str1 = *(char*)str2;
str1 = (char*)str1 + 1;
str2 = (char*)str2 + 1;
}
return ret;
}
int main() {
int a[20] = { 0 };
int b[] = { 1,2,3,4,5,6,7 };
myMemcpy(a, b, 8);
return 0;
}