memchr
Locate byte in byte string
Description
The memchr function locates the first occurrence ofc
(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 withinlen
bytes.