1 memmove函数的实现
#include <stdio.h>
#include <string.h>void* my_memmove(void*dest, const void*src, size_t len)
{ char *pdest = (char *)dest; const char* psrc = (char *)src; if ((psrc < pdest) && (pdest < psrc + len)) { pdest = pdest + len - 1;psrc = psrc + len - 1;while (len--) {printf("*pstr is %c and *pdest is %c\n", *psrc, *pdest);*pdest-- = *psrc--; } } else { while (len--) { *pdest++ = *psrc++; } } return dest;
} int main()
{char s1[10] = "chenyu";printf("dest is %s\n", s1);printf("src is %s\n", s1 - 3);memmove(s1, s1 - 3, 6);printf("s1 is %s\n", s1 + 3);char s2[10] = "chenyu";my_memmove(s2, s2 - 3, 6);printf("s2 is %s\n", s2 + 3