memchr, wmemchr
Find characters in a buffer.
void *memchr( const void *buf, int c, size_t count ); // C only void *memchr( void *buf, int c, size_t count ); // C++ only const void *memchr( const void *buf, int c, size_t count ); // C++ only wchar_t *wmemchr( const wchar_t * buf, wchar_t c, size_t count ); // C only wchar_t *wmemchr( wchar_t * buf, wchar_t c, size_t count ); // C++ only const wchar_t *wmemchr( const wchar_t * buf, wchar_t c, size_t count ); // C++ only
Parameters
- buf
-
Pointer to buffer.
- c
-
Character to look for.
- count
-
Number of characters to check.
memchr and wmemchr look 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.
In C, these functions take a const pointer for the first argument. In C++, two overloads are available. The overload taking a pointer to const returns a pointer to const; the version that takes a pointer to non-const returns a pointer to non-const. The macro _CONST_CORRECT_OVERLOADS is defined if both the const and non-const versions of these functions are available. If you require the non-const behavior for both C++ overloadsin C++, define the symbol _CONST_RETURN.
| Routine | Required header | Compatibility |
|---|---|---|
| memchr | <memory.h> or <string.h> | ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 |
| wmemchr | <wchar.h> | ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 |
For more information about compatibility, see Compatibility.
Libraries
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 );
else
printf( "Result: %c not found\n" );
}
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.