C++ wmemcpy()

The wmemcpy() function in C++ copies a specified number of wide characters from source to the destination.

The wmemcpy() function is defined in <cwchar> header file.

wmemcpy() prototype

wchar_t* wmemcpy( wchar_t* dest, const wchar_t* src, size_t count );

The wmemcpy() function takes three arguments: dest, src and count. This function when called, copies exactly count wide characters from the memory location pointed to by src to the memory location pointed to by dest.

The behaviour of this function is undefined if:

  • Either src or dest is a null pointer.
  • The objects overlaps.

wmemcpy() Parameters

  • dest: Pointer to the wide character array where the contents are copied to
  • src: Pointer to the wide character array where the contents are copied from.
  • count: Number of wide characters to copy from src to dest.

wmemcpy() Return value

  • The wmemcpy() function returns dest.

Example: How wmemcpy() function works?

#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;

int main()
{
	setlocale(LC_ALL, "en_US.utf8");
	
	wchar_t src[] = L"\u03b1\u03b2\u03b3\u03b8\u03bb\u03c9\u03c0";
	wchar_t dest[20];
	int count = 5;
	
	wmemcpy(dest, src, count);
	wcout << L"After copying" << endl;
	
	for(int i=0; i<count; i++)
		putwchar(dest[i]);
	
	return 0;
}

When you run the program, the output will be:

After copying
αβγθλ
Did you find this article helpful?