C memmove() Function

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

Definition

In C, memmove() copies n bytes from one place to another. Unlike memcpy(), the source and destination may overlap, so it is safe for shifting bytes inside one array.

You pass the destination, the source, and the count. All three are required. The result is the destination pointer. Include <string.h>.

Syntax

Pass destination, source, and count. Start with this call.

C
memmove(buf + 1, buf, 4);
CallWhat it does
memmove(buf + 1, buf, 4)Shifts 4 bytes one place to the right inside the same array.

Include <string.h>. Prefer this over memcpy() when the regions might overlap.

Library declarations
C
void *memmove(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 — Shift a String Left (Overlap)

Remove the first character by sliding the rest left. Source and destination share one buffer, so use memmove.

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

int main(void) {
    char s[] = "Hello";

    /* slide "ello\0" onto s[0] */
    memmove(s, s + 1, strlen(s));
    printf("%s\n", s);

    return 0;
}

How It Works

  1. Start with "Hello". Bytes are H e l l o \0.
  2. Count with strlen. strlen(s) is 5, so five bytes move from s + 1.
  3. Slide left. Those bytes are e l l o \0, so s becomes "ello".
  4. Overlap needs memmove. Using memcpy for the same slide would be undefined.

Example 2 — Copy Between Separate Buffers

When regions do not overlap, memmove still works. Prefer memcpy if you know they are disjoint.

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

int main(void) {
    const char *src = "Code";
    char dest[16];

    memmove(dest, src, strlen(src) + 1);
    printf("dest = %s\n", dest);

    return 0;
}

How It Works

  1. Include the null. strlen("Code") + 1 is 5 bytes.
  2. Copy into dest. Letters and '\0' land in dest.
  3. Print as a string. Because the terminator was copied, %s works.
  4. Overlap not required. memmove is still correct; memcpy would also be fine here.

Example 3 — Shift an Integer Array Right

Open a hole at index 0 by sliding elements one slot right. Copy backward-style overlap needs memmove.

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

int main(void) {
    int a[5] = { 10, 20, 30, 40, 0 };
    int i;

    /* shift first 4 ints one place right */
    memmove(a + 1, a, 4 * sizeof(int));
    a[0] = 5;

    for (i = 0; i < 5; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");

    return 0;
}

How It Works

  1. Start. Array is {10, 20, 30, 40, 0}.
  2. Move four ints right. memmove(a+1, a, 4*sizeof(int)) yields {10, 10, 20, 30, 40} temporarily—overlap is handled so values are not lost.
  3. Write the new head. a[0] = 5 → {5, 10, 20, 30, 40}.
  4. Why memmove? memcpy here would be undefined because a and a+1 overlap.

Frequently Asked Questions

memmove() copies n bytes from src to dest, even when the regions overlap.
All three are required. dest, src, and n work like memcpy, but overlap is allowed.
The same dest pointer you passed in.
Include <string.h>.
Use memmove() whenever dest and src might overlap, such as when shifting bytes in one array.

Next: memset()

Fill a memory block with a single byte value.

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