用char*指针实现memcpy函数!
// MyMemcpy.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include<iostream>
/***********************************************************
vs2015中测试通过,其他环境可能需要将_snprintf_s换成_snprintf
************************************************************/
void* MyMemcpy(void* dst, void* src, size_t len)
{
if (NULL == src || NULL == src || 0 == len)
return dst;
char* tmpsrc = (char*)src;
char* tmpdst = (char*)dst;
while (len-- > 0)
{
*tmpdst++ = *tmpsrc++;
}
return dst;
}
//测试对象A
struct A
{
int nTest;
char szTest[50];
A()
{
nTest = 0;
memset(szTest, 0, sizeof(szTest));
}
};
int main()
{
A a, b;
a.nTest = 1;
_snprintf_s(a.szTest, sizeof(a.szTest) - 1, "abcd f");
MyMemcpy(&b, &a, sizeof(A));
std::cout << b.nTest << std::endl << b.szTest << std::endl;
getchar();
return 0;
}