memchr, wmemchr
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

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
);

Parameters

buf
Pointer to buffer.
c
Character to look for.
count
Number of characters to check.

Return Value

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

Remarks

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.

Requirements

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.

Libraries

All versions of the C run-time libraries.

Example

// 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 );
   else
      printf( "Result:      %c not found\n" );
}

Output

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

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

Show:
© 2016 Microsoft