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
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
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);
}
|