memchr() — Search Buffer

Format

#include <string.h>
void *memchr(const void *buf, int c, size_t count);

Language Level: ANSI

Threadsafe: Yes.

Description

The memchr() function 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

The memchr() function 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 that uses memchr()

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" );
  }
}
 
/********************  Output should be similar to:  **************
 
The string starting with x is xing
*/

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]