( standard c libraries translation )memcpy家族

本文详细介绍了C语言中常用的字符串操作函数,包括memcpy、memccpy、memchr、memrchr、rawmemchr、memcmp、memmove及memset的功能、用法及示例。这些函数用于复制、比较和填充内存区域,并在内存中搜索特定字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

memcpy - copy memory area
memcpy-复制内存

所需头文件
#include <string.h>

void *memcpy(void *dest, const void *src, size_t n);

The  memcpy() function copies n bytes from memory area src to memory area dest.  The memory areas must not overlap.  Use memmove(3) if the memory areas do overlap.
memcpy()函数从src复制n字节到dest,内存区域不能重叠,如果内存区域有重叠,请使用memmove

The memcpy() function returns a pointer to dest.
memcpy返回一个指向dest的指针


memccpy - copy memory area
memccpy-复制内存

所需头文件
#include <string.h>

void *memccpy(void *dest, const void *src, int c, size_t n);

The memccpy() function copies no more than n bytes from memory area src to memory area dest, stopping when the character c is found. If the memory areas overlap, the results are undefined.
memccpy()函数从src最多复制n字节到dest,当发现了字符c的时候停止,如果内存区域有重叠,结果是未定义的

The memccpy() function returns a pointer to the next character in dest after c, or NULL if c was not found in the first n characters of src.
memccpy()函数返回一个指针,指针指向dest中c的下一个字符,如果c在src的前n个字符中没有出现则返回NULL


memchr, memrchr, rawmemchr - scan memory for a character
memchr,memrchr,rawmemchr-在内存中扫描一个字符

所需头文件
#include <string.h>
void *memchr(const void *s, int c, size_t n);
void *memrchr(const void *s, int c, size_t n);
void *rawmemchr(const void *s, int c);

The  memchr()  function  scans  the initial n bytes of the memory area pointed to by s for the first instance of c.  Both c and the bytes of the memory area pointed to by s are interpreted as unsigned char.
memchr()函数在内存s中的前n个字节中扫描字符c第一次出现的位置,字符c和内存的字节都被当作unsigned char型

The memrchr() function is like the memchr() function, except that it searches backward from the end of the n bytes pointed to by s instead  of  forward from the beginning.
memrchr()函数跟memchr相似,memrchr是前n字节反向查找,而不是正向查找

The rawmemchr() function is similar to memchr(): it assumes (i.e., the programmer knows for certain) that an instance of c lies somewhere in the memory area starting at the location pointed to by s, and so performs an optimized search for c (i.e., no use of a count argument to limit the  range  of  the search).   If an instance of c is not found, the results are unpredictable.  The following call is a fast means of locating a string's terminating null byte:
    char *p = rawmemchr(s, '\0');
rawmemchr函数类似与memchr,它假设字符c一定存在与s指针所指向的内存中,然后选择最优化的算法来查找c(没有使用数量参数来限制查找位置),如果没有找到c,结果是不可预知的,下面的调用是一种快速的方法找到字符串结束字符\0:
    char *p = rawmemchr(s, '\0');


返回值
The memchr() and memrchr() functions return a pointer to the matching byte or NULL if the character does not occur in the given memory area.
The rawmemchr() function returns a pointer to the matching byte, if one is found.  If no matching byte is found, the result is unspecified.
memchr()和memrchr()函数返回一个指向匹配字符的指针,如果在给出的范围内没有找到匹配的字符则返回NULL,如果找到一个,rawmemchr()函数返回一个指向匹配字符的指针,如果没有找到,结果是不可预知的。


memcmp - compare memory areas
memcmp-比较内存区域

所需头文件
#include <string.h>

int memcmp(const void *s1, const void *s2, size_t n);

The  memcmp()  function compares the first n bytes (each interpreted as unsigned char) of the memory areas s1 and s2.  It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.
memcmp函数比较内存区域s1和s2的前n个字节,返回一个整型小于,等于,大于0,如果s1逐个的小于,等于,大于s2


memmove - copy memory area
memmove-复制内存区域

所需头文件
#include <string.h>

void *memmove(void *dest, const void *src, size_t n);

The  memmove() function copies n bytes from memory area src to memory area dest.  The memory areas may overlap: copying takes place as though the bytes in src are first copied into a temporary array that does not overlap src or dest, and the bytes are then copied from the temporary array to dest.
memmove()函数复制src中的n字节到dest,内存区域可能会重叠:复制会把src的数据复制到一个不与src和dest重叠的临时的数组中,然后把这些字节从临时数组中复制到dest

The memmove() function returns a pointer to dest.
memmove返回指向dest的指针


memset - fill memory with a constant byte
用常量字节填充内存

#include <string.h>

void *memset(void *s, int c, size_t n);

The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.
memset()函数用常量字节c填充内存区域s的前n个字节
The memset() function returns a pointer to the memory area s.

memset()函数返回指向内存区域s的指针


testcase如下:

#include <stdio.h>
#include <string.h>

int main(void)
{
	const char *src = "abcxxscba";
	char dest[10], tmp[10];
	char *ret;
	int result;

	memcpy(dest, src, strlen(src) + 1);
	printf("dest = %s\n", dest);

	memset(tmp, 'x', 9);
	tmp[9] = 0;
	ret = memccpy(tmp, src, 'c', strlen(src) + 1);
	printf("dest = %s\n", tmp);
	printf("ret = %s\n", ret);

	ret = memchr(src, 'b', 10);
	printf("ret = %s\n", ret);

	ret = memrchr(src, 'b', 10);
	printf("ret = %s\n", ret);

	ret = rawmemchr(src, 'b');
	printf("ret= %s\n", ret);
	
	result = memcmp(dest, src, 10);
	printf("result = %d\n", result);

	memset(dest, 0x00, 10);
	memmove(dest, src, 5);
	printf("dest = %s\n", dest);
	return 0;
}

这里面memrchr和rawmemchr这个在编译的时候需要带上-D_GNU_SOURCE编译参数,不然会有warning报出,或者#define _GNU_SOURCE加在第一行(必须要第一行才行)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值