标签:实现memmove
实现memmove
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<assert.h> void *my_memmove(void *dest, const void *src, size_t count) { char *ret = NULL; char *pdest = (char *)dest; char *psrc = (char *)src; ret = dest;//转化成指针类型 assert(dest); assert(src); if ((pdest<psrc + count) && (pdest>psrc))//从后往前拷贝 { while (count--) { pdest[count] = psrc[count]; //*(pdest+count)=*(psrc+count); } } else //从前往后拷贝 { while (count--) { *(pdest++) = *(psrc++); } } return ret; } int main() { int i = 0; int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; my_memmove(arr + 3, arr + 1, 4 * 4); for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) { printf("%d ", arr[i]); } printf("\n"); system("pause"); return 0; }
运行结果:
1 2 3 2 3 4 5 8 9
请按任意键继续. . .
本文出自 “10910765” 博客,请务必保留此出处http://10920765.blog.51cto.com/10910765/1746914
标签:实现memmove
原文地址:http://10920765.blog.51cto.com/10910765/1746914