C++ 之手寫memcpy

#include<iostream>
#include<cstdio>
using namespace std;ios


void* mymemcpy(void* dst, const void* src, size_t n){
if (dst == NULL || src == NULL) return NULL;
char* pdst;
char* psrc;c++

if (src >= dst || (char*)dst >= (char*)src + n - 1){
pdst = (char*)dst;
psrc = (char*)src;spa

while (n--){
*pdst++ = *psrc++;
}
}
else{
pdst = (char*)dst+ n - 1;
psrc = (char*)src+ n - 1;io

while (n--){
*pdst-- = *psrc--;
}
}stream

return dst;
}psr

 

int main(){di

char buf[100] = "abcdefghijk";
memcpy(buf+2, buf, 5);
//mymemcpy(buf + 2, buf, 5);
printf("%s\n", buf + 2);while

return 0;
}co

 

結果 
abcdehijk

return

相關文章
相關標籤/搜索