#include <iostream>
#include <assert.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
//拷贝函数
struct {
char name[40];
int age;
} person,person_copy;
void* My_memcpy(void* dest,const void* src,size_t num)
{
assert(dest && src); //判断dest和src存在
void* ret = dest; //返回赋值对象
while(num--)
{
*(char*)dest = *(char*)src; //将src按字节赋值给dest
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}
int main()
{
char myname[] = "Pierre de Fermat";
My_memcpy(person.name,myname,strlen(myname) + 1); //name赋值,按字符传值,+1 是因为数组最后一位\0
person.age = 46; //age赋值
My_memcpy(&person_copy,&person,sizeof(person)); //对象拷贝
printf("person_copy:%s,%d \n",person_copy.name,person_copy.age);
return 0;
}