C library function - memchr()
Description
C library function **void memchr(const void str, int c, size_t n) Searches the first str bytes of the string pointed to by the parameter for the first occurrence of the character n (an unsigned character) cand returns its position.
Declaration
Here is the declaration of the memchr() function.
void *memchr(const void *str, int c, size_t n)
Parameters
- str -- a pointer to the memory block in which to perform the search.
- c -- the value passed as an int, but used in its unsigned char form for each byte searched.
- n -- the number of bytes to be analyzed.
Return Value
This function returns a pointer to the matching byte, or NULL if the character does not appear in the given memory area.
Example
The following example demonstrates the use of the memchr() function.
Example
#include
<
stdio.h
#include
<
string.h
int
main
(
)
{
const
char
str
[
]
=
"
http://www.runoob.com
";
const
char
ch
= '.';
char
ret
;
ret
=
(
char
)
memchr
(
str
,
ch
,
strlen
(
str
)
)
;
printf
(
"
The string after |%c| is - |%s|
\
n
"
,
ch
,
ret
)
;
return
(
0
)
;
}
Let's compile and run the above program, which will produce the following result:
|.| 之后的字符串是 - |.runoob.com|
C Standard Library -