#include "stdafx.h"
#include <iostream>
using namespace std;
char *memorycpy(char *dest, char *src, int len)
{
if (!dest || !src ||!len || (dest == src))
{
return dest;
}
char *pdest = dest;
char *psrc = src;
//目标地址高于源地址,从后往前复制
if (dest > src)
{
pdest = pdest + len - 1;
psrc = psrc + len - 1;
while(len--)
{
*pdest-- = *psrc--;
}
}
else //反过来,从前向后复制
{
while(len--)
{
*pdest++ = *psrc++;
}
}
return dest;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *s1 = new char[30];
char *s2;
for (int i = 0; i < 10; i++)
{
*(s1 + i) = 'a' + i;
}
s2 = s1 + 2;
s2 = memorycpy(s2, s1, 3);
cout << s2 << endl;
delete [] s1;
return 0;
}