C String memmove() Function

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
<string.h>

What You’ll Learn

The memmove() function from <string.h> copies n bytes from source to destination—just like memcpy()—but it is safe when the two regions overlap. That makes it the right choice for shifting text inside one buffer or any in-place memory rearrangement.

01

Copy n Bytes

Like memcpy.

02

Overlap Safe

Key difference.

03

Returns dest

Same pointer.

04

Shift In Place

Same buffer.

05

+1 for Strings

Include \0.

06

vs memcpy()

When to pick each.

Definition and Usage

In C, memmove() is declared in <string.h>. It transfers n bytes from src to dest and returns dest. When the ranges overlap, the implementation copies in the correct direction so source bytes are not overwritten before they are read.

Use memmove() whenever source and destination might be the same array or share memory—for example, removing the first character from a string or inserting bytes in the middle of a buffer. When regions are guaranteed disjoint, memcpy() is also valid and may be marginally faster.

💡
Beginner Tip

When in doubt, choose memmove(). It behaves like memcpy() for non-overlapping copies but stays defined when overlap occurs—a common situation in string editing and buffer compaction.

📝 Syntax

The standard library declaration:

C
void *memmove(void *dest, const void *src, size_t n);

Parameters

  • dest — pointer to the destination buffer (must hold at least n bytes).
  • src — pointer to the source data.
  • n — number of bytes to copy.

Return Value

Returns dest (the destination pointer).

Header

#include <string.h>

⚡ Quick Reference

ScenarioCallNotes
Copy C-string (separate buffers)memmove(d, s, strlen(s)+1)Includes '\0'
Shift string left in placememmove(s, s+1, len)Overlap: use memmove
Shift string right in placememmove(s+1, s, len+1)Make room at front
Non-overlapping copymemcpy(d, s, n)Also valid if disjoint
Return valuedestSame as memcpy
Safe copy
memmove(d, s, n)

Overlap allowed

In-place
memmove(buf, buf+k, n)

Same buffer shift

String
strlen(s) + 1

Copy null byte too

Disjoint only
memcpy(d, s, n)

No overlap

Examples Gallery

Compile with gcc memmove.c -std=c11 -o memmove. Examples progress from simple copies to overlap-safe in-place shifts.

📚 Getting Started

Copy a string into a separate destination buffer.

Example 1 — Basic Copy to a Separate Buffer

Copy "Hello, C!" when source and destination do not overlap.

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

int main(void) {
    char source[] = "Hello, C!";
    char destination[20];

    memmove(destination, source, strlen(source) + 1);

    printf("Source:      %s\n", source);
    printf("Destination: %s\n", destination);

    return 0;
}

How It Works

Here memmove behaves like memcpy because the buffers are separate. strlen(source) + 1 copies the null terminator so destination is a valid C-string.

Example 2 — Shift Text Left (Remove First Character)

Move "bcdef" to the start of the same buffer—classic overlapping copy.

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

int main(void) {
    char text[] = "abcdef";

    memmove(text, text + 1, strlen(text));
    /* copies "bcdef\0" starting at text[0] */

    printf("After shift left: %s\n", text);

    return 0;
}

How It Works

Source (text + 1) and destination (text) overlap. memmove copies forward safely. Using memcpy(text, text + 1, ...) here would be undefined behavior.

📈 Practical Patterns

Insert space, edit in place, and compare with memcpy.

Example 3 — Shift Text Right (Make Room at Front)

Move content right by one byte to insert a character later.

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

int main(void) {
    char text[] = "world";
    size_t len = strlen(text);

    memmove(text + 1, text, len + 1);
    text[0] = 'H';

    printf("Result: %s\n", text);

    return 0;
}

How It Works

Destination starts at a higher address than source, so memmove copies backward internally to avoid overwriting unread bytes. After the shift, we write 'H' at index 0.

Example 4 — Remove a Character From the Middle

Close a gap after deleting one character from a string.

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

int main(void) {
    char word[] = "Hello";
    size_t removeAt = 1;  /* remove 'e' at index 1 */

    memmove(word + removeAt, word + removeAt + 1,
            strlen(word) - removeAt);

    printf("After removal: %s\n", word);

    return 0;
}

How It Works

Bytes from index 2 onward shift left to index 1, including the trailing '\0'. This is a typical text-editor “delete character” pattern built on overlap-safe copying.

Example 5 — When memcpy() Is Enough

Use memcpy only when regions are guaranteed not to overlap.

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

int main(void) {
    char src[] = "Copy me";
    char dst[16];

    /* Safe: separate arrays, no overlap */
    memcpy(dst, src, strlen(src) + 1);
    printf("memcpy result:  %s\n", dst);

    char buffer[] = "overlap";
    /* Unsafe if uncommented — src and dest overlap: */
    /* memcpy(buffer, buffer + 2, 6); */

    memmove(buffer, buffer + 2, 6);
    printf("memmove result: %s\n", buffer);

    return 0;
}

How It Works

memcpy is fine for independent buffers. For in-buffer edits like buffer + 2buffer, always use memmove. The shifted result is "erlap" (first two characters overwritten by the shift).

🚀 Common Use Cases

  • In-place string edits — delete or insert characters without a second buffer.
  • Buffer compaction — shift live data to the start of an array after removing headers.
  • Safe generic copy — use when overlap cannot be ruled out in library code.
  • Ring buffers — rearrange bytes when linearizing circular queue contents.
  • Defensive coding — prefer memmove when pointer arithmetic makes overlap possible.

🧠 How memmove() Works

1

You pass dest, src, and n

Same parameters as memcpy—destination, source, byte count.

Input
2

Check for overlap

If ranges overlap, copy backward or forward so source bytes are preserved until read.

Safety
3

Copy n bytes

Transfer completes and dest is returned.

Transfer
=

Destination updated

Memory rearranged safely—even when src and dest share the same array.

📝 Notes

  • memmove is always safe for overlap; memcpy is not.
  • Destination must still have room for n bytes—overflow rules apply like memcpy.
  • For C-strings, copy strlen(s) + 1 unless you intentionally handle termination separately.
  • When shifting right, ensure the buffer is large enough for the expanded content.
  • For disjoint copies where performance is critical, memcpy may be preferred after proving no overlap.

⚡ Optimization

memmove() may add a small overhead compared to memcpy() because it must handle overlap. Use memcpy in hot paths only when you have proven the regions are disjoint. Otherwise, the safety of memmove outweighs the tiny cost difference in most applications.

Conclusion

memmove() is the overlap-safe memory copy in C. Reach for it when editing strings in place, compacting buffers, or whenever source and destination might alias the same memory. Keep using memcpy() for separate, non-overlapping blocks when you need maximum clarity about intent.

Practice the in-place shift examples, then continue to memset() for filling memory with a byte value.

💡 Best Practices

✅ Do

  • Use memmove for in-place string edits
  • Size dest for at least n bytes
  • Copy strlen(s) + 1 for full C-strings
  • Prefer memmove when overlap is possible
  • Null-terminate after partial shifts if needed

❌ Don’t

  • Use memcpy on overlapping regions
  • Shift right without enough buffer capacity
  • Forget the null byte when building C-strings
  • Assume memmove allocates memory
  • Ignore buffer bounds when computing n

Key Takeaways

Knowledge Unlocked

Five things to remember about memmove()

Use these points whenever you copy or shift memory in C.

5
Core concepts
🔒 02

Overlap Safe

Main advantage.

Safety
🔢 03

Shift In Place

Same buffer.

Pattern
📈 04

+1 for \0

String copies.

Strings
💬 05

vs memcpy()

Disjoint only.

Compare

❓ Frequently Asked Questions

memmove() copies n bytes from src to dest, like memcpy(), but works correctly even when the two regions overlap. It returns dest. Use it when source and destination may share the same buffer or partially overlap.
void* memmove(void* dest, const void* src, size_t n); Include <string.h>. dest is the destination buffer, src is the source, and n is the number of bytes to copy.
memcpy() requires non-overlapping regions—overlap is undefined behavior. memmove() detects overlap and copies in the safe direction (forward or backward) so data is not clobbered before it is read.
Use memmove when copying within the same array, shifting text left or right, or whenever you are not 100% sure the regions are disjoint. Use memcpy only when regions definitely do not overlap—it may be slightly faster.
Copy strlen(src) + 1 bytes to include the null terminator: memmove(dest, src, strlen(src) + 1). Ensure dest has enough space. When src and dest are the same buffer and overlap, memmove is required.
It returns dest—the destination pointer you passed in. The return value matches memcpy's behavior and is mainly useful for chaining copies in advanced code.
Did you know?

On many platforms, memmove checks whether dest falls inside the source range and chooses a forward or backward copy. When there is no overlap, it may delegate to the same fast path as memcpy—so using memmove everywhere is often reasonable for clarity.

Continue the String Functions Series

Master overlap-safe copies with memmove(), then learn filling buffers with memset().

Next: memset() →

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.

6 people found this page helpful