目录
1.memcpy函数
(1)函数定义
void * memcpy ( void * destination, const void * source, size_t num );
source是指向要拷贝的元素的指针,num是要操作的字节数,destination是拷贝的空间的指针。
memcpy的作用是将从source开始的num个字节的元素拷贝到destination数组中。
#include <stdio.h>
#include <string.h>
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
int str[100] = { 0 };
int * pa = memcpy(str, arr + 2, 24);
int i = 0;
for (i = 0; i < 6; i++)
{
printf("%d ", pa[i]);
}
printf("\n");
return 0;
}
输出结果为:
在该代码中,source是arr+2,arr是数组首元素地址(即1的地址),arr+2是3的地址。num是24,即复制从3开始往后24个字节的数字到destination中去,这里的destination是str数组。
注意事项:
- memcpy函数遇到'\0'后并不会停下来;
- 如果source和destination的内容发生重叠,则复制的结果是未定义的。
(2)memcpy函数的实现
memcpy函数拷贝的是内存,因此他的返回类型因该是一个指针类型,而memcpy操作的即可以是char类型,又可以是int类型,因此形参得用void*类型的指针,由于传入的是字节数,我们可以通过将void*类型转换为char*类型,然后将source中的每个字节传递到destination中。
#include <stdio.h>
#include <assert.h>
void* my_memcpy(void* destination, const void* source, size_t num)
{
assert(source);
while (num--)
{
*(char*)destination = *(char*)source;
destination = (char*)destination + 1;
source = (char*)source + 1;
}
return destination;
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
int str[100] = { 0 };
my_memcpy(str, arr + 2, 24);
int i = 0;
for (i = 0; i < 6; i++)
{
printf("%d ", str[i]);
}
printf("\n");
return 0;
}
输出结果:
2.memmove函数
由于当两个数组的内存发生重叠是memcpy不能够处理,所以memmove就应运而生了。
(1)定义
void * memmove ( void * destination, const void * source, size_t num );
(2)使用
#include <stdio.h>
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
memmove(arr + 3, arr, 24);
for (int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
输出结果:
(3)memmove的实现
memmove和memcpy相比只是多了一个自我拷贝的过程。在模拟实现该函数的时候我们应该要注意:
情况一:
要将arr指向的内容拷贝到str指向的位置如果直接从前向后拷贝,则b,c就会被覆盖,拷贝就会出错。因此我们得从后往前拷贝。
情况二:
在该情况中,要将arr指向的内容拷贝到str指向的内容中,这时只能从前向后拷贝,若从后向前拷贝,则arr中的内容就会被覆盖,造成拷贝失败。
情况三:
在这两种情况中,不论从前向后拷贝还是从后向前拷贝都没影响。
代码如下:
#include <stdio.h>
#include <assert.h>
void* my_memmove( void* str, const void* arr, size_t num)
{
assert(arr);
if (arr < str)
{
str = (char*)str + num - 1;
arr = (char*)arr + num - 1;
while (num--)
{
*(char*)str = *(char*)arr;
str = (char*)str - 1;
arr = (char*)arr - 1;
}
}
else
{
while (num--)
{
*(char*)str = *(char*)arr;
str = (char*)str + 1;
arr = (char*)arr + 1;
}
}
return str;
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
my_memmove(arr + 3, arr, 24);
for (int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
输出结果:
3.memset函数
(1)定义
void * memset ( void * ptr, int value, size_t num );
从ptr指向的第一个字节开始向后num个字节数设为指定值(value)。
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "hello world";
memset(str + 6, 'x', 5);
printf(str);
return 0;
}
输出结果:
4.memcmp函数
比较从ptr1和ptr2内存开始,向后num个字节。
(1)定义
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
将ptr1指向的num个字节与ptr2指向的num个字节进行比较,若两内存块内容相等则返回0;若在两个内存块中不匹配的第一个字节在ptr1中的值低于ptr2中的值(如果计算为无符号字符值),则返回小于0的值;若在两个内存块中不匹配的第一个字节在ptr1中的值大于ptr2中的值(如果计算为无符号字符值),则返回大于0的值。
(2)使用
#include <stdio.h>
#include <string.h>
int main()
{
char buffer1[] = "DWgaOtP12df0";
char buffer2[] = "DWGAOTP12DF0";
int n;
n = memcmp(buffer1, buffer2, sizeof(buffer1));
if (n > 0)
printf("'%s' is greater than '%s'.\n", buffer1, buffer2);
else if (n < 0)
printf("'%s' is less than '%s'.\n", buffer1, buffer2);
else
printf("'%s' is the same as '%s'.\n", buffer1, buffer2);
return 0;
}
输出结果为: