C++ Standard Library C++ STL Library

C++ <cwchar> - wmemcpy() Function



The C++ <cwchar> wmemcpy() function is used to copy num wide characters of memory block pointed to by source to the memory block pointed to by destination. The behavior of the function is undefined if destination and source overlaps.

Syntax

wchar_t* wmemcpy (wchar_t* destination, const wchar_t* 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 wide characters to copy.
size_t is an unsigned integer type.

Return Value

Returns destination

Example:

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

#include <iostream>
#include <cwchar>
using namespace std;
 
int main (){
  wchar_t str[50] = L"Hello World!";
  wchar_t new_str[50];
  
  //copying 5 wide characters of memory 
  //blocks str into memory block new_str
  wmemcpy(new_str, str, 5);

  //null wide character manually 
  //added to new_str
  new_str[5] = L'\0';

  //displaying the result
  wcout<<"new_str contains: "<<new_str;

  return 0;
}

The output of the above code will be:

new_str contains: Hello

❮ C++ <cwchar> Library