//模拟实现memmove函数(考虑内存重叠) #include <stdio.h> #include <assert.h> #include <string.h> void * memmove(void * dst, const void * src, int count) { void * ret = dst; assert(dst); assert(src); if (dst <= src || (char *)dst >= ((char *)src + count)) //正常情况不发生重叠,从低到高 { while (count--) { *(char *)dst = *(char *)src; dst = (char *)dst + 1; src = (char *)src + 1; } } else //发生重叠,从低到高 { dst = (char *)dst + count - 1; src = (char *)src + count - 1; while (count--) { *(char *)dst = *(char *)src; dst = (char *)dst - 1; src = (char *)src - 1; } } return ret; } int main() { char p[] = "hello!"; char *q = "world"; memmove(p, q,strlen(q)+1 ); printf("%s ", p); return 0; }