C++ memccpy() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cstring>

Definition

memccpy copies bytes from source to destination until it has copied a specified byte, or until n bytes are copied.

It is POSIX and in C23. On some Windows toolchains it may be missing—examples show the idea with a small portable loop when needed.

This guide shows the C library helper in a C++ .cpp program with std::cout. Prefer std::string for new text code; keep these APIs for C interop and interviews.

Syntax

When available:

C++
// POSIX / C23:
// void* p = memccpy(dest, src, c, n);
CallWhat it does
memccpy(dest, src, c, n)Copy until byte c is copied or n reached.

Return value points just after the copied c, or is null if c was not copied within n bytes.

Library declarations
C++
void* memccpy(void* dest, const void* src, int c, size_t n);

Parameters

ParameterDescription
dest, srcDestination and source.
cStop byte (after it is copied).
nMaximum bytes to copy.

Return Value

Return ValueDescription
pointer or nullPast the copied stop byte, else nullptr.

C++ Examples

Example 1 — Teach the memccpy Idea

Copy until comma with a loop (portable).

C++
#include <iostream>

int main() {
    const char src[] = "ab,cd";
    char dest[8] = {};
    std::size_t i = 0;
    for (; i < sizeof src - 1 && i < 7; ++i) {
        dest[i] = src[i];
        if (src[i] == ',') break;
    }
    std::cout << dest << '\\n';
    return 0;
}

How It Works

  1. We copy characters including the comma.
  2. Loop stops after copying ','.
  3. Printed buffer is ab,

Example 2 — Stop by Count

Copy at most two bytes.

C++
#include <cstring>
#include <iostream>

int main() {
    char src[] = "WXYZ";
    char dest[5] = {};
    std::memcpy(dest, src, 2);
    std::cout << dest << '\\n';
    return 0;
}

How It Works

  1. Only two bytes are taken.
  2. Null from initialization keeps it printable.
  3. Shows limiting a copy by count.

Example 3 — Copy Without Hit

When stop byte is absent, copy n bytes.

C++
#include <cstring>
#include <iostream>

int main() {
    char src[] = "abcd";
    char dest[5] = {};
    std::memcpy(dest, src, 4);
    std::cout << dest << '\\n';
    return 0;
}

How It Works

  1. No early stop in this demo.
  2. Four bytes are copied.
  3. Result is abcd.

Frequently Asked Questions

A copy that stops after copying a given byte or after n bytes.
It is POSIX/C23; C++ support depends on the library.
Pointer past the stop byte, or null if not found within n.
A small loop or memcpy with your own scan.
Use std::string.

Next: memchr()

Keep going through the string functions. Search for a byte in a memory block.

memchr() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

13 people found this page helpful