C++ wmemset()

The wmemset() function in C++ copies a single wide character for a specified number of time to a wide character array.

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

wmemset() prototype

wchar_t* wmemset( wchar_t* dest, wchar_t ch, size_t count );

The wmemset() function takes three arguments: dest, ch and count. The wide character represented by ch is copied into the first count characters of the wide character array pointed to by dest.

The behaviour of the function is undefined if overflow occurs. If count is zero, this function does nothing.


wmemset() Parameters

  • dest: Pointer to the wide character array to copy the wide character.
  • ch: The wide character to copy.
  • count: Number of times to copy.

wmemset() Return value

  • The wmemset() function returns dest.

Example: How wmemset() function works?

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

int main()
{
	setlocale(LC_ALL, "en_US.utf8");
	
	wchar_t ch = L'\u2206';
	wchar_t dest[20];
	int count = 10;
	
	wmemset(dest, ch, count);
	wcout << L"After copying " << ch << L" 10 times" << endl;
	
	for(int i=0; i<count; i++)
		putwchar(dest[i]);
	
	return 0;
}

When you run the program, the output will be:

After copying ∆ 10 times
∆∆∆∆∆∆∆∆∆∆