标签:
//模拟实现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\n", p); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/doudouwa1234/article/details/46739725