C memmove() Function

Last Updated:

C memmove()

The memmove() function copies a block of memory from a location to another.

This function copies the data first to an intermediate buffer, then from buffer to destination. So, memmove() is slightly slower approach than memcpy().

This function doesn't overlap the source. So, memmove() is safer than memcpy().

Example

C Compiler
#include <stdio.h> #include <string.h> int main() { char dest[] = "abcde"; char src[] = "123"; memmove(dest, src, 3); printf("New dest = %s", dest); return 0; }

Syntax

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

Parameter Values

ValueTypeExplanation
dest Required Destination array where the content is to be copied.
src Required Source of data to be copied.
n Required Number of bytes to be copied.

Return Value

ValueExplanation
Address Returns a pointer to the destination, which is dest array.

More Examples

In the following example, the destination string is "cde" and the source string is "abcde". So, the memmove() function will replace "cde" with "abc" and the result is ababc.

Example

C Compiler
#include <stdio.h> #include <string.h> int main() { char str1[] = "abcde"; memmove(&str1[2], &str1[0], 3); printf("New str1 = %s", str1); return 0; }

In the following example, memcpy() overlaps the source, but memove() doesn't.

Example

C Compiler
#include <stdio.h> #include <string.h> int main() { char str1[] = "abcde"; char str2[] = "abcde"; memmove(&str1[2], &str1[0], 3); memcpy(&str2[2], &str2[0], 3); printf("memmove's str1 = %s\n", str1); printf("memcpy's str2 = %s\n", str2); return 0; }

Share this Page

Meet the Author