C memcpy() Function

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

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.

C
memcpy(dest, src, 6);
CallWhat it does
memcpy(dest, src, 6)Copies 6 bytes from src into dest.

Include <string.h>. For overlapping regions, use memmove().

Library declarations
C
void *memcpy(void *dest, const void *src, size_t n);

Parameters

ParameterDescription
destRequired. Where the bytes are written. It must hold at least n bytes.
srcRequired. Where the bytes are copied from.
nRequired. How many bytes to copy.

Return Value

Return ValueDescription
dest pointerThe 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.

C
#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

  1. Measure the string. strlen("Hello") is 5; add 1 for '\0' → n = 6.
  2. Copy six bytes. Letters and the terminator move into dest.
  3. Print as a string. Because '\0' was copied, %s works.
  4. 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.

C
#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

  1. Use sizeof for n. sizeof src is the full array size in bytes (often 12).
  2. Same layout. Three ints are copied as raw bytes into dest.
  3. Matching capacity. dest[3] matches src’s element count.
  4. 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).

C
#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

  1. Pass addresses. &b and &a are the destinations and sources.
  2. Size with sizeof. sizeof a covers every byte of the struct (including padding).
  3. Fields appear in b. After the copy, b.x and b.y match a.
  4. 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.

Next: memmove()

Copy bytes safely even when source and destination overlap.

memmove() 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