0Pricing
C Academy · Lesson

memcpy and memset

Raw memory functions.

memcpy and memset is a free C Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Raw memory functions

Unlike string functions, memcpy and memset work on raw bytes and ignore null terminators.

They are declared in <string.h> and operate on a count of bytes you specify, making them general-purpose for any data type.

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

int main(void) {
    int src[] = {1, 2, 3};
    int dest[3];
    memcpy(dest, src, sizeof(src));
    printf("%d %d %d\n", dest[0], dest[1], dest[2]);
    return 0;
}

The memcpy signature

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

It copies exactly n bytes from src to dest and returns dest. Use sizeof to get the right byte count.

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

int main(void) {
    char src[] = "copy me";
    char dest[16];
    memcpy(dest, src, strlen(src) + 1);
    printf("%s\n", dest);
    return 0;
}

Counting bytes correctly

The count is in bytes, not elements. For an array of int, you must multiply by sizeof(int).

Using sizeof(array) on a real array gives the total byte size automatically.

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

int main(void) {
    double a[4] = {1.5, 2.5, 3.5, 4.5};
    double b[4];
    memcpy(b, a, 4 * sizeof(double));
    printf("%.1f %.1f\n", b[0], b[3]);
    return 0;
}

Regions must not overlap

memcpy has undefined behavior if the source and destination regions overlap.

When ranges might overlap (for example shifting elements within one array), use memmove instead, which handles overlap safely.

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

int main(void) {
    char buf[] = "ABCDEF";
    memmove(buf + 2, buf, 4);
    printf("%s\n", buf);
    return 0;
}

The memset signature

void *memset(void *ptr, int value, size_t n);

It fills n bytes at ptr with the byte value. The value is interpreted as an unsigned char, so only the lowest 8 bits matter.

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

int main(void) {
    char buf[6];
    memset(buf, 'x', 5);
    buf[5] = '\0';
    printf("%s\n", buf);
    return 0;
}

Zeroing memory

The most common use of memset is clearing a buffer or struct to all zeros before use.

memset(arr, 0, sizeof(arr)) sets every byte to 0, which is a valid zero for ints, floats, and pointers.

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

int main(void) {
    int counts[5];
    memset(counts, 0, sizeof(counts));
    for (int i = 0; i < 5; i++) printf("%d ", counts[i]);
    printf("\n");
    return 0;
}

memset works byte by byte

Be careful: memset sets each byte, not each element. Using it to fill an int array with the value 1 does NOT produce ones.

Each int would get every byte set to 0x01, giving 0x01010101, not 1.

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

int main(void) {
    int a[2];
    memset(a, 1, sizeof(a));
    printf("%d\n", a[0]);
    return 0;
}

Clearing a struct

memset is handy to initialize a whole struct to zero in one call, instead of setting each field manually.

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

struct Point { int x; int y; int z; };

int main(void) {
    struct Point p;
    memset(&p, 0, sizeof(p));
    printf("%d %d %d\n", p.x, p.y, p.z);
    return 0;
}

Copying structs

You can copy a struct with memcpy, though plain assignment (b = a;) usually does the same thing more clearly.

memcpy is preferred when copying through generic void * pointers where types are erased.

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

struct Rec { int id; char tag; };

int main(void) {
    struct Rec a = {42, 'A'};
    struct Rec b;
    memcpy(&b, &a, sizeof(a));
    printf("%d %c\n", b.id, b.tag);
    return 0;
}

memcmp: comparing memory

A related function, memcmp(a, b, n), compares n raw bytes and returns 0 if they match.

Unlike strcmp, it does not stop at a null terminator, so it works on any binary data.

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

int main(void) {
    int a[] = {1, 2, 3};
    int b[] = {1, 2, 3};
    if (memcmp(a, b, sizeof(a)) == 0)
        printf("Arrays have identical bytes\n");
    return 0;
}

Why these are fast

memcpy and memset are often hand-optimized in assembly to move many bytes per instruction.

They are usually far faster than a hand-written byte loop, so prefer them for bulk data movement and initialization.

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

int main(void) {
    char big[1000];
    memset(big, 0, sizeof(big));
    printf("Cleared %lu bytes\n", sizeof(big));
    return 0;
}

Quick Check

Test your understanding of memset's byte-level behavior.

Recap

You learned the raw memory functions:

  • memcpy copies n bytes; regions must not overlap (use memmove if they might).
  • memset fills n bytes with one byte value; great for zeroing.
  • Counts are in bytes — use sizeof and beware byte-vs-element pitfalls.
  • memcmp compares raw bytes without stopping at '\0'.

Frequently asked questions

Is the “memcpy and memset” lesson free?

Yes — the full text of “memcpy and memset” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “memcpy and memset”?

Raw memory functions. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “memcpy and memset” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. strlen, strcpy, strcat
  2. strcmp and Comparison
  3. strtok and Tokenizing
  4. memcpy and memset
← Back to C Academy