Home C String Functions memccpy() C memccpy() Function Definition In C, memccpy() copies bytes from one memory block to another. It stops after copying the first byte equal to c, or after copying n bytes when that byte is not found.
Unlike string-copy functions, memccpy() works with raw bytes and does not stop at '\0' unless zero is the chosen stop byte. It is a POSIX function, not a classic ISO C function available on every compiler.
Syntax Start with this concrete call, then adjust the values for your buffer.
char *after = memccpy(dest, src, ':', sizeof src); Call What it does memccpy(dest, src, ':', n)Copies through the first colon, but never more than n bytes.
Include <string.h> on systems that provide it. The source and destination must not overlap.
Library declaration void *memccpy(void *restrict dest,
const void *restrict src,
int c, size_t n); Parameters Parameter Description destRequired. Writable destination with room for at least n bytes. srcRequired. Source memory block containing at least n readable bytes. cThe stop byte. The value is converted to unsigned char before comparison. nMaximum number of bytes to examine and copy.
Return Value Return Value Description Pointer after stop byteWhen c is found, points one byte past its copied position in dest. NULLReturned when c is not found in the first n source bytes.
C Examples 1. Copy through delimiter 2. Stop byte missing 3. Binary packet Example 1 — Copy Through a Delimiter Copy a name and its colon, then use the returned pointer to finish a C-string.
#include <stdio.h>
#include <string.h>
int main(void) {
const char src[] = "name:value";
char dest[16];
char *after = memccpy(dest, src, ':', sizeof src);
if (after != NULL) {
*after = '\0';
printf("%s\n", dest);
}
return 0;
} How It Works Start at the first byte. memccpy begins copying n, a, m, and e.Copy the delimiter. The colon is copied into dest before the function stops.Use the return pointer. after points to the byte immediately after the copied colon.Terminate the text. Writing '\0' through after makes dest safe for printf.Example 2 — Stop Byte Not Found See the NULL result when the requested byte is absent.
#include <stdio.h>
#include <string.h>
int main(void) {
const char src[] = "ABCDE";
char dest[6] = {0};
void *result = memccpy(dest, src, 'X', 5);
printf("result: %s\n", result == NULL ? "NULL" : "found");
printf("copied: %s\n", dest);
return 0;
} result: NULL
copied: ABCDE How It Works Set a strict limit. Only the first five bytes of src are examined.Search while copying. Each byte is copied even though none equals 'X'.Reach the limit. After five copies, the function cannot continue and returns NULL.Keep termination separate. The zero-filled destination supplies a terminator after the copied bytes.Example 3 — Copy a Binary Record The stop value can be a byte in binary data; no C-string is required.
#include <stdio.h>
#include <string.h>
int main(void) {
unsigned char packet[] = {10, 20, 255, 30, 40};
unsigned char out[5] = {0};
unsigned char *after = memccpy(out, packet, 255, sizeof packet);
size_t copied = after ? (size_t)(after - out) : sizeof packet;
printf("bytes copied = %zu\n", copied);
printf("last byte = %u\n", out[copied - 1]);
return 0;
} bytes copied = 3
last byte = 255 How It Works Treat data as bytes. The arrays contain numbers, not null-terminated text.Compare converted values. The stop value 255 matches the third unsigned byte.Subtract pointers. after - out gives the number of bytes copied.Include the matched byte. The count is three because the matching byte is copied before stopping.Frequently Asked Questions What does memccpy() do in C? It copies up to n bytes and stops just after copying the first byte equal to c.
What does memccpy() return? It returns a pointer one byte past the copied stop byte, or NULL when that byte is not found.
Does memccpy() add a null terminator? No. It only copies bytes. Add a terminator yourself if the result must be a C-string.
Is memccpy() standard C? It is specified by POSIX and is not a classic ISO C function available everywhere.
Can source and destination overlap? No. Use memmove() when the memory regions may overlap.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion