liveBook · Manning

Memchr

As explained in Modern C, Third Edition

memchr is a C standard-library function that searches a specified memory region for a byte value.

memchr searches a specified memory region for a byte value. It takes the region, the value to find, and an explicit length, so the search is expressed over an array or memory range rather than as a string operation. section 6.1.5

Function form

The standard-library interface is declared as: void* memchr(const void *s, int c, size_t n); Here, s identifies the memory region, c supplies the byte value being searched for, and n supplies the region length.

TAKEAWAY

  • Use memchr when the search range is described by an explicit length. section 6.1.5
  • Distinguish memchr from string functions such as strlen and strcpy, which operate on null-terminated strings. section 6.1.5

How Modern C, Third Edition covers it

How deep it goes
Defines the three-argument interface and search purpose, but does not develop implementation internals or edge-case behavior.
Examples
Provides the memchr prototype alongside memcpy and memcmp prototypes.
What it assumes
Assumes familiarity with arrays, character data, pointers, and explicit size arguments.
What you can do after
Apply via the C standard-library API to search a specified memory range for a byte value.
How it connects
Connects memchr with memcpy and memcmp as memory functions, and contrasts them with null-terminated string functions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.