On this page
memchr
Prev: memcmp | Up: string.h | Next: strlen
Rel: string.h, stdlib.h & string.h Guide
Ref: cppreference — memchr
Tags: #public #prog #c #stdlib #string #tier/standard
Include
#include <string.h>
Signature & Overview
void *memchr(const void *ptr, int ch, size_t n);
Finds the first occurrence of (unsigned char)ch within the first n bytes of ptr (or NULL).
Notes & Pitfalls
- Binary-safe byte scan (does not stop at
'\0', unlikestrchr()): ReturnsNULLif not found withinnbytes.
Example
#include <assert.h>
#include <string.h>
int main(void) {
const char buf[] = {'a', '\0', 'Z'};
const char *hit = memchr(buf, 'Z', sizeof(buf));
assert(hit == &buf[2]);
}
Linked from
Related notes
- tuts/c/stdlib/string/memcmp Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strlen Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string
- tuts/c/stdlib/stdlib_and_string_h Linked here; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/string/memcpy shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memmove shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memset shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strcat shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strchr shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strcmp shared tags: #c, #prog, #stdlib, #string, #tier