Skip to content
Tutorials Center
My learning

Topics

C Reference ยท string.h

C memchr() function

Reference for C memchr(): find the first occurrence of a byte in a memory block, how it differs from strchr(), and how to loop over every match.

memchr() searches the first n bytes of a memory block for a byte value and returns a pointer to the first match, or NULL if the byte does not occur. The value you search for is converted to unsigned char, and every byte is compared as an unsigned char.

It is the memory counterpart of strchr. Because you give it an explicit length, it does not stop at a null byte, so it works on binary data and on text buffers that are not terminated. It also never reads past n bytes, which makes it a safe way to look for a delimiter in a fixed-size buffer.

C
#include <string.h>

void *memchr(const void *s, int c, size_t n);

Try it

C Finding a byte and its index

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Visiting every match

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
void *memchr(const void *s, int c, size_t n);

Before C23 the function accepts const void * and returns a non-const void *, which quietly drops the const. C23 makes it qualifier-preserving: passing a pointer to const gives back a pointer to const.

Name Description
s The start of the block to search.
c The byte to look for. It is converted to unsigned char.
n The number of bytes to examine.

Returns: a pointer to the first byte equal to (unsigned char)c, or NULL if none of the first n bytes matches.

Errors / undefined behavior: s must point to at least n readable bytes (reading stops early when a match is found, so only the bytes up to the match must be valid). Passing NULL is undefined, even when n is 0.

Usage notes

  • Offset, not pointer. Subtract the start from the result to get an index: (size_t)((const char *)hit - (const char *)buf). Always test for NULL first.
  • Length matters. The n argument is a byte count, not an element count. For an int array, pass count * sizeof(int), and remember you are searching for one byte, not one integer.
  • Not for finding strings or integers. memchr looks for a single byte. To search for a substring use strstr; the C standard has no memmem, although GNU libc and BSD systems provide one as an extension.
  • strchr comparison. strchr stops at the terminator and can find the terminator itself; memchr stops after n bytes. Use memchr when the buffer may not be terminated, when it contains embedded zero bytes, or when you already know the length.
  • Searching for zero. memchr(buf, 0, n) finds the first null byte within n bytes. It is a bounded alternative to strlen, and it is how you check whether a buffer really holds a string.
  • Performance. The cost is linear in the distance to the match. Library versions are heavily optimized and usually compare a whole machine word at a time, so they are faster than a hand-written loop.
  • Reverse search. Standard C has no memrchr. GNU libc has one; for portable code write a loop that walks backwards.
  • Standard version. Available since C89. C23 makes the pointed-to type follow the qualifier of the argument.