w3resource

C memmove() function

C memmove() function - copy bytes in memory with overlapping areas

Syntax:

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

The memmove() function is used to copy n bytes of src to dest.

Parameters:

Name Description Required /Optional
dest Destination object. Required
src Source object. Required
n Number of bytes. Optional

Return value from memviod()

The memmove() function shall return dest. No return value is reserved to indicate an error.

Example: memmove() function.

This example copies the word specific part from position target + 2 to position target + 8.

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

#define SIZE  45 
char target[SIZE] = "The quick brown fox jumps over the lazy dog."; 
int main( void )
{
  char * p = target + 8;  /* p points at the starting character
                          of the word we want to replace */
  char * source = target + 2;   
  printf( "Before memmove, target is \"%s\"\n", target );
  memmove( p, source, 5 );
  printf( "After memmove, target becomes \"%s\"\n", target );
}

Output:

Before memmove, target is "The quick brown fox jumps over the lazy dog."
After memmove, target becomes "The quice quiwn fox jumps over the lazy dog."

Example that uses memmove()

Following example copies the contents of source to target.

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

#define MAX_LEN 80
 
char str1[ MAX_LEN ] = "This is the string1";
char str2[ MAX_LEN ] = "This is the string2";
 
int main(void)
{
  printf("Before memmove, strings are:");
  printf("\n%s",str1);
  printf("\n%s",str2);  
  memmove(str2, str1, sizeof(str1));
  printf( "\n\nAfter memmove, strings are:");
  printf("\n%s",str1);
  printf("\n%s",str2);  
}

Output:

Before memmove, strings are:
This is the string1
This is the string2

After memmove, strings are:
This is the string1
This is the string1

C Programming Code Editor:

Previous C Programming: C memcpy()
Next C Programming: C memset()



Follow us on Facebook and Twitter for latest update.