Find characters in a buffer.
#include <memory.h> | Required only for function declarations | |
#include <string.h> | Use either STRING.H (for ANSI compatibility) or MEMORY.H |
void *memchr( const void *buf, int c, size_t count );
void __far * __far _fmemchr( const void __far *buf, int c, size_t count );
buf | Pointer to buffer | |
c | Character to look for | |
count | Number of characters |
The memchr and _fmemchr functions look for the first occurrence of c in the first count bytes of buf. They stop when they find c or when they have checked the first count bytes.
The _fmemchr function is a model-independent (large-model) form of the memchr function. It can be called from any point in any program.
If successful, memchr or _fmemchr returns a pointer (or a far pointer) to the first location of c in buf. Otherwise, they return NULL.
memchr
Standards:ANSI, UNIX
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
_fmemchr
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:None
_memccpy, memcmp, memcpy, memset, strchr
/* 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";
void main( void )
{
char *pdest;
int result;
printf( "String to be searched:\n\t\t%s\n", string );
printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );
printf( "Search char:\t%c\n", ch );
pdest = memchr( string, ch, sizeof( string ) );
result = pdest - string + 1;
if( pdest != NULL )
printf( "Result:\t\t%c found at position %d\n\n", ch, result );
else
printf( "Result:\t\t%c not found\n" );
}
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