locate byte in byte string

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
Workings

\code #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.
", pos - values); else printf("Value 3 not found.
"); return 0; } \endcode

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.

SEE ALSO

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

STANDARDS

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