I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingCString.h

memchr

Locate byte in byte string
+ View other 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:

Example - Locate byte in byte string
Workings
#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;
}
Solution
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.

Standards

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