Format
#include <string.h> /* also in <memory.h> */ void *memchr(const void *buf, int c, size_t count);
Language Level: ANSI, XPG4, Extension
memchr searches the first count bytes of buf for the
first occurrence of c converted to an unsigned character. The
search continues until it finds c or examines count bytes.
Return Value
memchr returns a pointer to the location of c in buf.
It returns NULL if c is not within the first count bytes of buf.
Example
This example finds the first occurrence of "x"
in the string that you provide. If it is found, the string that
starts with that character is printed.
#include <stdio.h> #include <string.h>
int main(int argc, char ** argv)
{
char * result;
if ( argc != 2 )
printf( "Usage: %s string\n", argv[0] );
else
{
if ((result = (char *) memchr( argv[1], 'x', strlen(argv[1])) ) != NULL)
printf( "The string starting with x is %s\n", result );
else
printf( "The letter x cannot be found in the string\n" );
}
return 0;
/**************************************************************************
If the program is passed the argument ' boxing',
the output should be:
The string starting with x is xing **************************************************************************/ }
![]()
memccpy -- Copy Bytes
memcmp -- Compare Buffers
memcpy -- Copy Bytes
memicmp -- Compare Bytes
memmove -- Copy Bytes
memset -- Set Bytes to Value
strchr -- Search for Character
<memory.h>
<string.h>