Easy Tutorial
❮ C Function Strcoll C Function Strncpy ❯

C Library Function - memchr()

C Standard Library - <string.h>

Description

The C library function void *memchr(const void *str, int c, size_t n) searches for the first occurrence of the character c (an unsigned char) in the first n bytes of the string pointed to by the argument str.

Declaration

Here is the declaration for the memchr() function.

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

Parameters

Return Value

The 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.

#include <stdio.h>
#include <string.h>

int main ()
{
   const char str[] = "http://www.tutorialpro.org";
   const char ch = '.';
   char *ret;

   ret = (char*)memchr(str, ch, strlen(str));

   printf("String after |%c| is - |%s|\n", ch, ret);

   return(0);
}

Let's compile and run the above program, which will produce the following result:

String after |.| is - |.tutorialpro.org|

C Standard Library - <string.h>

❮ C Function Strcoll C Function Strncpy ❯