void* memcpy(void* destination, const void* source, size_t num);
#include
#include
int main()
{
int arr1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int arr2[20] = { 0 };
//将arr1中的内容,拷贝到arr2中
memcpy(arr2, arr1, 40);
// int* int*
int i = 0;
for (i = 0; i < 20; i++)
{
printf("%d ", arr2[i]);//1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0
}
return 0;
}
#include
#include
int main()
{
float arr1[] = { 1.0, 2.0, 3.0 };
float arr2[5] = { 0 };
//将arr1中的内容,拷贝到arr2中
memcpy(arr2, arr1, 8);
// float* float*
int i = 0;
for (i = 0; i < 5; i++)
{
printf("%f ", arr2[i]);//1.000000 2.000000 0.000000 0.000000 0.000000
}
return 0;
}
memcpy的模拟实现:
#include
#include
//函数拷贝结束后,返回目标空间的起始地址
void* my_memcpy(void* dest, const void* src, size_t num)
{
void* ret = dest;
assert(dest && src);
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
//强制类型转换是临时的,不是永久的
}
return ret;
}
int main()
{
int arr1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int arr2[20] = { 0 };
my_memcpy(arr2, arr1, 20);
int i = 0;
for (i = 0; i < 20; i++)
{
printf("%d ", arr2[i]);//1 2 3 4 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
}
return 0;
}
如果目标空间和源头空间有重合,就会出现以下情况:
#include
#include
//函数拷贝结束后,返回目标空间的起始地址
void* my_memcpy(void* dest, const void* src, size_t num)
{
void* ret = dest;
assert(dest && src);
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
//强制类型转换是临时的,不是永久的
}
return ret;
}
int main()
{
int arr1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
my_memcpy(arr1 + 2, arr1, 20);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);//1 2 1 2 1 2 1 8 9 10
}
return 0;
}
void* memmove(void* destination, const void* source, size_t num);
#include
#include
int main()
{
int arr1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
memmove(arr1 + 2, arr1, 20);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);//1 2 1 2 3 4 5 8 9 10
}
return 0;
}
#include
#include
void* my_memmove(void* dest, const void* src, size_t num)
{
void* ret = dest;
assert(dest && src);
if (dest < src)
{
//前->后
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{
//后->前
while (num--)
{
*((char*)dest + num) = *((char*)src + num);
}
}
return ret;
}
int main()
{
int arr1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
my_memmove(arr1 + 2, arr1, 20);//1 2 1 2 3 4 5 8 9 10
//my_memmove(arr1, arr1 + 2, 20);//3 4 5 6 7 6 7 8 9 10
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
return 0;
}
int memcmp(const void* ptr1, const void* ptr2, size_t num);
#include
#include
int main()
{
int arr1[] = { 1, 2, 1, 4, 5, 6 };
int arr2[] = { 1, 2, 257 };
//int ret = memcmp(arr1, arr2, 9);
//printf("%d\n", ret);//0
int ret = memcmp(arr1, arr2, 10);
printf("%d\n", ret);//-1
return 0;
}
void* memset(void* ptr, int value, size_t num);
#include
#include
int main()
{
char arr[] = "hello bit";
memset(arr + 1, 'x', 4);//以字节为单位设置的
printf("%s\n", arr);//hxxxx bit
return 0;
}
#include
int main()
{
int arr[10] = { 0 };
memset(arr, 1, 10);
return 0;
}