C memcpy() Function

Definition
In C, memcpy() copies n bytes from one place in memory to another. It copies raw bytes. It does not add a '\0' for you.
You pass the destination, the source, and the count. All three are required. The two regions must not overlap. Include <string.h>.
Syntax
Pass destination, source, and count. Start with this call.
| Call | What it does |
|---|
memcpy(dest, src, 6) | Copies 6 bytes from src into dest. |
Include <string.h>. For overlapping regions, use memmove().
Library declarations
void *memcpy(void *dest, const void *src, size_t n);
Parameters
| Parameter | Description |
|---|
| dest | Required. Where the bytes are written. It must hold at least n bytes. |
| src | Required. Where the bytes are copied from. |
| n | Required. How many bytes to copy. |
Return Value
| Return Value | Description |
|---|
| dest pointer | The same destination pointer you passed in. |
C Examples
Example 1 — Copy a C-String (Include '\0')
Use strlen(src) + 1 so the terminator is copied. Prefer strcpy for everyday string copies; this shows the byte count clearly.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *src = "Hello";
char dest[16];
memcpy(dest, src, strlen(src) + 1);
printf("dest = %s\n", dest);
return 0;
}
How It Works
- Measure the string.
strlen("Hello") is 5; add 1 for '\0' → n = 6. - Copy six bytes. Letters and the terminator move into
dest. - Print as a string. Because
'\0' was copied, %s works. - Size dest first.
dest[16] is larger than 6 bytes, so the write is safe.
Example 2 — Copy an Integer Array
Multiply element count by sizeof(int) so the byte count stays portable.
#include <stdio.h>
#include <string.h>
int main(void) {
int src[] = { 10, 20, 30 };
int dest[3];
int i;
memcpy(dest, src, sizeof src);
for (i = 0; i < 3; i++) {
printf("%d ", dest[i]);
}
printf("\n");
return 0;
}
How It Works
- Use sizeof for n.
sizeof src is the full array size in bytes (often 12). - Same layout. Three
ints are copied as raw bytes into dest. - Matching capacity.
dest[3] matches src’s element count. - No string rules. There is no
'\0' involved—just a block copy.
Example 3 — Copy a Struct
Copy one struct into another with a single call. Both objects must be the same type (same size and layout).
#include <stdio.h>
#include <string.h>
struct Point {
int x;
int y;
};
int main(void) {
struct Point a = { 3, 4 };
struct Point b;
memcpy(&b, &a, sizeof a);
printf("b = (%d, %d)\n", b.x, b.y);
return 0;
}
How It Works
- Pass addresses.
&b and &a are the destinations and sources. - Size with sizeof.
sizeof a covers every byte of the struct (including padding). - Fields appear in b. After the copy,
b.x and b.y match a. - Assignment is fine too.
b = a; is clearer for one struct; memcpy shines for buffers and arrays.
Frequently Asked Questions
memcpy() copies n bytes from src to dest. It returns the destination pointer.
All three are required. dest is where bytes are written, src is the source, and n is the count.
The same dest pointer you passed in.
Include <string.h>.
No. Use memmove() when dest and src may overlap.
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