memmove Function

Copies n characters of the source buffer into the destination buffer, even if the regions overlap.

Include

<string.h>

Prototype

void *memmove(void *s1, const void *s2, size_t n);

Arguments

s1
buffer to copy characters to (destination)
s2
buffer to copy characters from (source)
n
number of characters to copy from s2 to s1

Return Value

Returns a pointer to the destination buffer.

Remarks

If the buffers overlap, the effect is as if the characters are read first from s2, then written to s1, so the buffer is not corrupted.

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

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

int main(void)
{
  char buf1[50] = "When time marches on";
  char buf2[50] = "Where is the time?";
  char buf3[50] = "Why?";

  printf("buf1 : %s\n", buf1);
  printf("buf2 : %s\n", buf2);
  printf("buf3 : %s\n\n", buf3);

  memmove(buf1, buf2, 6);
  printf("buf1 after memmove of 6 chars of "
         "buf2: \n\t%s\n", buf1);

  printf("\n");

  memmove(buf1, buf3, 5);
  printf("buf1 after memmove of 5 chars of "
         "buf3: \n\t%s\n", buf1);
}

Example Output

buf1 : When time marches on
buf2 : Where is the time?
buf3 : Why?

buf1 after memmove of 6 chars of buf2:
        Where ime marches on

buf1 after memmove of 5 chars of buf3:
        Why?