strcpy和memcpy函数的实现
2015-04-29 15:10
176 查看
#include <cstdio> char* strcpy1(char *des,const char *src) //自己的 { if(des== NULL || src== NULL) return NULL; char *pDes= des; char *pSrc= (char*)src; while((*pDes++= *pSrc++)!= '\0'); return pDes; } void* memcpy1(void *des, const void *src, unsigned int len) //自己的 { if(des== NULL || src== NULL) return NULL; char *pDes= (char*)des; char *pSrc= (char*)src; if(pDes<= pSrc || pDes>= pSrc+ len) { while(len--) *pDes++= *pSrc++; } else { pDes+= len- 1; pSrc+= len- 1; while(len--) *pDes--= *pSrc--; } return des; } void *mymemcpy(void *dst,const void *src,size_t num) //别人的改进 { //assert((dst!=NULL)&&(src!=NULL)); int wordnum = num/4;//计算有多少个32位,按4字节拷贝 int slice = num%4;//剩余的按字节拷贝 int * pintsrc = (int *)src; int * pintdst = (int *)dst; while(wordnum--)*pintdst++ = *pintsrc++; while (slice--)*((char *)pintdst++) =*((char *)pintsrc++); return dst; } int main() { char ch[20]="123"; char cc[20]; strcpy1(cc, ch); printf("%s %s\n",cc,ch); memcpy1(cc+3,ch,4); printf("%s %s\n",cc,ch); memcpy1(cc+1, cc, 7); printf("%s\n",cc); int a[]={1,2,3,4,5}; int b[20]; memcpy1(b, a, sizeof(int)*5); //mymemcpy(b, a, sizeof(int)*5); for(int i= 0; i< 5; i++) printf("%d\n",b[i]); return 0; }
相关文章推荐
- strcpy和memcpy函数的实现及其区别
- (C++面试题)请实现strcpy、memcpy函数
- 自我实现strcpy,memcpy函数
- strcpy、memcpy函数的实现
- strcpy和memcpy函数的实现
- 不用库函数,自己实现strcpy和memcpy函数
- 编程实现strcpy和memcpy函数
- 不用库函数,自己实现strcpy和memcpy函数;二者的区别
- 自我实现strcpy,memcpy函数
- 面试中长间的strcpy、字符串整形相互转换、memcpy函数的实现
- C语言中strlen、strcpy、strncpy、strcat、strnact、strcmp、memcpy函数的实现
- strcpy,memcpy函数的实现
- C语言中strcpy与memcpy函数实现与区别
- 【19】实现strcpy和memcpy函数
- C中strcpy与memcpy函数实现与区别
- C语言中strlen、strcpy、strncpy、strcat、strnact、strcmp、memcpy函数的实现
- strlen,strcat,strcpy,strncpy,strcmp的具体实现
- memcpy, strcpy, strlen, strcmp, itoa, atoi 的函数实现
- 面试基础_03实现strcpy、strcat、strcmp、strlen
- strcpy以及memcpy的实现