C++ Standard Library C++ STL Library

C++ <cstring> - memmove() Function



The C++ <cstring> memmove() function is used to copy num characters of memory block pointed to by source to the memory block pointed to by destination.

This method can be used even when the two memory blocks overlap. This is because copying takes place as if an intermediate buffer is created where the data are first copied to from source and then finally copied to destination.

The behavior of the function is undefined if either destination or source is a null pointer.

Syntax

void * memmove ( void * destination, const void * source, size_t num );

Parameters

destination Specify pointer to the first block of memory where the content is to be copied.
source Specify pointer to the second block of memory to copy from.
num Specify number of characters to copy.
size_t is an unsigned integer type.

Return Value

Returns destination

Example:

The example below shows the usage of memmove() function.

#include <iostream>
#include <cstring>
using namespace std;
 
int main (){
  char str[50] = "1234567890";

  //displaying content of str
  cout<<"str contains: "<<str<<"\n";

  //copying first 5 characters of str
  memmove(str + 5, str, 5);

  //displaying the result
  cout<<"str contains: "<<str;

  return 0;
}

The output of the above code will be:

str contains: 1234567890
str contains: 1234512345

❮ C++ <cstring> Library