C memchr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

Definition

In C, memchr() searches a block of memory for the first byte that matches a value. It looks at exactly n bytes. It does not stop early at a '\0'.

You pass the memory, the byte to find, and how many bytes to scan. All three are required. The result is a pointer to the match, or NULL when it is missing. Include <string.h>.

Syntax

Pass the block, the byte, and the count. Start with this call.

C
memchr(buf, 'A', 5);
CallWhat it does
memchr(buf, 'A', 5)Looks at the first 5 bytes of buf for the byte 'A'.

Include <string.h>. Check the result for NULL before you use the pointer.

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

Parameters

ParameterDescription
sRequired. The memory to search.
cRequired. The byte to find, often written as a character in single quotes.
nRequired. How many bytes to scan.

Return Value

Return ValueDescription
PointerPoints to the first matching byte inside the same block.
NULLReturned when that byte is not found in the first n bytes.

C Examples

Example 1 — Find a Character and Its Index

Search for 'C' in "Hello, C!" and print the zero-based index.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *str = "Hello, C!";
    char needle = 'C';
    void *p = memchr(str, needle, strlen(str));

    if (p != NULL) {
        size_t index = (const char *)p - str;
        printf("'%c' found at index %zu\n", needle, index);
    } else {
        printf("'%c' not found\n", needle);
    }

    return 0;
}

How It Works

  1. Measure the string. strlen(str) is 9, so memchr scans only the visible characters.
  2. Search for the byte. Each byte is compared to 'C' until index 7 matches.
  3. Check for NULL. A non-null result means a match exists inside the block.
  4. Get the index. Subtract the base pointer: (const char *)p - str → 7.

Example 2 — Search Only the First n Bytes

A smaller n can miss a byte that appears later in the buffer.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *text = "ABC-DEF";
    void *dash;

    dash = memchr(text, '-', 3);   /* only "ABC" */
    if (dash == NULL) {
        printf("No dash in first 3 bytes\n");
    }

    dash = memchr(text, '-', 4);   /* "ABC-" */
    if (dash != NULL) {
        printf("Dash at index %td\n", (const char *)dash - text);
    }

    return 0;
}

How It Works

  1. First call uses n = 3. Only "ABC" is scanned; '-' is at index 3, so the result is NULL.
  2. Second call uses n = 4. The range includes the dash at index 3.
  3. n is the boundary. memchr never stops at '\0'—only n limits the scan.
  4. Use this for prefixes. Handy when a buffer has a header length you must not read past.

Example 3 — Search a Binary Buffer

memchr() works when there is no null terminator—ideal for packets and raw bytes.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    unsigned char packet[] = { 0x01, 0x02, 0xFF, 0x04, 0x05 };
    void *marker = memchr(packet, 0xFF, sizeof packet);

    if (marker != NULL) {
        size_t offset = (unsigned char *)marker - packet;
        printf("0xFF at offset %zu\n", offset);
    } else {
        printf("Marker not found\n");
    }

    return 0;
}

How It Works

  1. Use sizeof for length. sizeof packet is 5—no string length needed.
  2. Search for a marker byte. 0xFF is found at offset 2.
  3. Pointer math on unsigned char*. Cast the result before subtracting from packet.
  4. Prefer memchr over strchr here. There is no '\0' end marker; strchr would be the wrong tool.

Frequently Asked Questions

memchr() searches the first n bytes of a memory block for a matching byte. It returns a pointer to the match, or NULL.
All three are required. s is the memory, c is the byte to find, and n is how many bytes to scan.
A pointer to the first match, or NULL when the byte is not found.
Include <string.h>.
No. It scans exactly n bytes, even if a '\0' appears earlier.

Next: memcmp()

Compare two memory blocks byte by byte with a length limit.

memcmp() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful