This documentation is archived and is not being maintained.

memchr, wmemchr

Finds characters in a buffer.

void *memchr(
   const void *buf,
   int c,
   size_t count
const wchar_t *wmemchr(
   const wchar_t * buf, 
   wchar_t c,
   size_t count


Pointer to buffer.
Character to look for.
Number of characters to check.

Return Value

If successful, returns a pointer to the first location of c in buf. Otherwise it returns NULL.


Looks for the first occurrence of c in the first count bytes of buf. It stops when it finds c or when it has checked the first count bytes.


Routine Required header Compatibility
memchr <memory.h> or <string.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
wmemchr <wchar.t> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_memchr.c

#include <memory.h>
#include <stdio.h>

int  ch = 'r';
char str[] =    "lazy";
char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] =   "         1         2         3         4         5";
char fmt2[] =   "12345678901234567890123456789012345678901234567890";

int main( void )
   char *pdest;
   int result;
   printf( "String to be searched:\n             %s\n", string );
   printf( "             %s\n             %s\n\n", fmt1, fmt2 );

   printf( "Search char: %c\n", ch );
   pdest = memchr( string, ch, sizeof( string ) );
   result = (int)(pdest - string + 1);
   if ( pdest != NULL )
      printf( "Result:      %c found at position %d\n", ch, result );
      printf( "Result:      %c not found\n" );


String to be searched:
             The quick brown dog jumps over the lazy fox
                      1         2         3         4         5

Search char: r
Result:      r found at position 12

See Also

Buffer Manipulation Routines | _memccpy | memcmp | memcpy | memset | strchr | Run-Time Routines and .NET Framework Equivalents