C memmove() Function

C memmove() Function

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
destRequiredDestination array where the content is to be copied.
srcRequiredSource of data to be copied.
nRequiredNumber of bytes to be copied.

Return Value

ValueExplanation
AddressReturns 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; }

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author