直接看代码
#include<stdio.h>
#include<string.h>
void* MemCopy(void* pDest, const void* pSrc, int nCount)
{char* p = (char*)pDest;char* p1 = (char*)pSrc;while (nCount--)*p++ = *p1++;return pDest;
}
void* MemMove(void* pDest, const void* pSrc, int nCount)
{char* dest = (char*)pDest;char* src = (char*)pSrc;if (dest > src && dest < src + nCount){src += nCount;dest += nCount;while (nCount--)*--dest = *--src;}else{while (nCount--)*dest++ = *src++;}return pDest;
}char str1[7] = "aabbcc";int main(void)
{printf("The string: %s\n", str1);MemMove(str1 + 2, str1, 4);printf("New string: %s\n", str1);strcpy_s(str1, sizeof(str1), "aabbcc"); printf("The string: %s\n", str1);memmove(str1 + 2, str1, 4);printf("New string: %s\n", str1);
}
输出结果