整理:memchr与strchr的区别

本文详细比较了memchr和strchr这两个C语言标准库函数的功能及使用场景。memchr用于在内存块中搜索特定字节,而strchr则是在字符串中搜索指定字符,直至遇到空字符结束。此外,文章还提供了这两个函数的源码实现。

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

1.

memchr检测的是一段内存,strchr检测的是一个字符串 如果一段内存中有0x0的话,显然不能用strchr去查找的。建议看看两个函数的原型


2.

strchr会停在\0,memchr不会,看接口就明白了:

NAME
       memchr, memrchr - scan memory for a character

SYNOPSIS
       #include <string.h>
       void *memchr(const void *s, int c, size_t n);
       void *memrchr(const void *s, int c, size_t n);

NAME
       strchr, strrchr - locate character in string

SYNOPSIS
       #include <string.h>
       char *strchr(const char *s, int c);
       char *strrchr(const char *s, int c);


3.

mem*系针对字节
str*系针对字符
2个概念


4.

mem  的效率高


5.

类似strcpy memcpy


6.

memchar针对与内存操作,shtchr只能是字符串操作


7.

C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\memchr.c

C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/***
*memchr.c - search block of memory for a given character
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       defines memchr() - search memory until a character is
*       found or a limit is reached.
*
*******************************************************************************/
 
#include <cruntime.h>
#include <string.h>
 
/***
*char *memchr(buf, chr, cnt) - search memory for given character.
*
*Purpose:
*       Searches at buf for the given character, stopping when chr is
*       first found or cnt bytes have been searched through.
*
*Entry:
*       void *buf  - memory buffer to be searched
*       int chr    - character to search for
*       size_t cnt - max number of bytes to search
*
*Exit:
*       returns pointer to first occurence of chr in buf
*       returns NULL if chr not found in the first cnt bytes
*
*Exceptions:
*
*******************************************************************************/
 
void  * __cdecl  memchr  (
         const  void  * buf,
         int  chr,
         size_t  cnt
         )
{
         while  ( cnt && (*(unsigned  char  *)buf != (unsigned  char )chr) ) {
                 buf = (unsigned  char  *)buf + 1;
                 cnt--;
         }
 
         return (cnt ? ( void  *)buf : NULL);
}

C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\strchr.c
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/***
*strchr.c - search a string for a given character
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       defines strchr() - search a string for a character
*
*******************************************************************************/
 
#include <cruntime.h>
#include <string.h>
 
/***
*char *strchr(string, c) - search a string for a character
*
*Purpose:
*       Searches a string for a given character, which may be the
*       null character '\0'.
*
*Entry:
*       char *string - string to search in
*       char c - character to search for
*
*Exit:
*       returns pointer to the first occurence of c in string
*       returns NULL if c does not occur in string
*
*Exceptions:
*
*******************************************************************************/
 
char  * __cdecl  strchr  (
         const  char  * string,
         int  ch
         )
{
         while  (*string && *string != ( char )ch)
                 string++;
 
         if  (*string == ( char )ch)
                 return (( char  *)string);
         return (NULL);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值