#include<stdlib.h> #include<stdio.h> #include<string.h> //memcpy的实现 int Mymem(char *dest, char *src, int size) { if (dest == NULL || src == NULL || size == 0) { return -1; } char *d = NULL; char *s = NULL; d = dest; s = src; //if ((unsigned char*)dst <= (unsigned char*)src|| (unsigned char *)dst >= ((unsigned char *)src + count)) //判断地址事是否重合 if (d > s && d < s+size) { d = dest + size; s = src + size; while (size--) { *d = *s; d--; s--; } } //不重合的情况直接将内存进行拷贝 else { while (size-- && *s != '\0') { *d++ = *s++; } } return 0; } void main() { //地址重合的情况 char p1[10] = "abcdef"; Mymem(p1 + 2, p1, strlen(p1)); printf("p1 = %s\n", p1); printf("p2 = %s\n", p1 + 2); //地址不重合的情况 char p3[10] = "abcdef"; char p4[100] = {0}; Mymem(p4, p3, strlen(p1)); printf("p3 = %s\n", p3); printf("p4 = %s\n", p4); system("pause"); }
memcpy用指针的实现通过判断地址是否重合解决掉字符串结束符('\0')问题,布布扣,bubuko.com
memcpy用指针的实现通过判断地址是否重合解决掉字符串结束符('\0')问题
原文地址:http://blog.csdn.net/han1558249222/article/details/25906689