On this page
  1. memchr
  2. Include
  3. Signature & Overview
  4. Notes & Pitfalls
  5. Example

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

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