locate byte in byte string

You're viewing an older version of this page (#3489). View the current version.

View versions (3)

INTERFACE

#include <string.h> void memchr ( const void *b, int c, size_t len )

DESCRIPTION

The memchr function locates the first occurrence of c (converted to an unsigned char) in string b.

Example 1

#include <stdio.h>
#include <string.h>

int main()
{
  // define a table of values
  unsigned char values[10] = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};

  // look for the value 3 and display search result
  unsigned char *pos = (unsigned char *)memchr(values, 3, 10);
  if (pos)
    printf("Value 3 found at position %d.\n", pos - values);
  else
    printf("Value 3 not found.\n");

  return 0;
}

Output:

Value 3 found at position 6.

RETURN VALUES

The memchr function returns a pointer to the byte located, or NULL if no such byte exists within len bytes.

SEE ALSO

strchr, strcspn, strpbrk, strrchr, strspn, strstr, strtok

STANDARDS

The memchr function conforms to ISO/IEC 9899:1990 ("ISO C90").