模拟实现memcpy,memmove函数
1.memmove
函数原型:void *memmove(void *dest, const void *source, size_t count)、
返回值说明:返回指向dest的void *指针
2.memcpy
函数原型:void *memcpy(void *dest, const void *source, size_t count);
返回值说明:返回指向dest的void *指针
//模拟实现memcpy函数
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void *my_memcpy(void *dest, void *src, int count)
{
char *pdest = (char *)dest;
char *psrc = (char *)src;
assert(dest);
assert(src);
void *tmp = (char *)dest;
while (count–)
{
*pdest++ = psrc++;
}
return tmp;
}
int main()
{
int a[6] = { 0 };
int b[6] = { 1, 2, 3, 4, 5, 6 };
int i = 0;
my_memcpy(a, b, 4sizeof(int));
for (i = 0; i <6; i++)
{
printf("%d “,a[i]);
}
system(“pause”);
return 0;
}
//模拟实现memmove函数
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void *memmove(void *dest, void *src, int count)
{
char *pdest = (char *)dest;
char *psrc = (char *)src;
assert(dest);
assert(src);
void *tmp = (char *)dest;
while (count–)
{
if (pdest > psrc)//从后向前拷贝
*(pdest + count) = *(psrc + count);
else
*pdest = *psrc;//从前向后拷贝
}
}
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int i = 0;
memmove(a + 1, a, sizeof(int));
for (i = 0; i < sizeof(a)/sizeof(a[0]); i++)
{
printf(”%d ",a[i]);
}
system(“pause”);
return 0;
}
07-07
800
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-08
3215
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-14
1063
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)