C Library Functions -memchr()

C Standard Library - <string.h>

Description

C Library Functionsvoid *memchr(const void *str, int c, size_t n)In the parameterstrthe beginning of the string pointed tonsearch for the first occurrence of a character in bytescthe position of (an unsigned character).

Declaration

Below is the declaration of the memchr() function.

void *memchr(const void *str, int c, size_t n)

Parameter

  • str-- points to the memory block to be searched.
  • c-- the value passed as an int, but the function uses the unsigned character form of this value in each byte search.
  • 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 usage of the memchr() function.

Example

#include <stdio.h> #include <string.h> int main () { const char str[] = "http://www.example.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 us compile and run the above program, which will produce the following result:

|.| 之后的字符串是 - |.example.com|

C Standard Library - <string.h>

other extensions