Easy Tutorial
❮ C Exercise Example16 C Standard Library Time H ❯

C Library Function - memmove()

C Standard Library - <string.h>

Description

The C library function void *memmove(void *str1, const void *str2, size_t n) copies n characters from str2 to str1. However, memmove() is a safer approach than memcpy() when dealing with overlapping memory blocks. If the target area and the source area overlap, memmove() ensures that the bytes in the overlapping area are copied to the target area before the source string is overwritten, altering the source area's content after the copy. If there is no overlap between the target area and the source area, it functions identically to memcpy().

Declaration

Below is the declaration for the memmove() function.

void *memmove(void *str1, const void *str2, size_t n)

Parameters

Return Value

This function returns a pointer to the destination storage area str1.

Example

The following example demonstrates the use of the memmove() function.

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

int main ()
{
   const char dest[] = "oldstring";
   const char src[]  = "newstring";

   printf("Before memmove dest = %s, src = %s\n", dest, src);
   memmove(dest, src, 9);
   printf("After memmove dest = %s, src = %s\n", dest, src);

   return(0);
}

Let's compile and run the above program, which will produce the following result:

Before memmove dest = oldstring, src = newstring
After memmove dest = newstring, src = newstring

C Standard Library - <string.h>

❮ C Exercise Example16 C Standard Library Time H ❯