Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C library function - memchr()

C library function - memchr()

C Standard Library - <string.h> C Standard Library -

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

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 - <string.h> C Standard Library -