C++ wcscpy()

The wcscpy() function in C++ copies a wide character string from source to destination.

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

wcscpy() prototype

wchar_t *wcscpy( wchar_t *dest, const wchar_t *src );

The wcscpy() function takes two arguments: dest and src. It copies the wide character string pointed to by src to the memory location pointed to by dest. The null terminating wide character is also copied.

The behaviour is undefined if:

  • The memory allocated for dest pointer is not large enough.
  • The strings overlap.

wcscpy() Parameters

  • dest: Pointer to a wide character array where the contents are copied to.
  • src: Pointer to a wide character array where the contents are copied from.

wcscpy() Return value

  • The wcscpy() function returns dest.

Example: How wcscpy() function works?

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

int main()
{
	setlocale(LC_ALL, "en_US.utf8");
	
	wchar_t src[] = L"\u0102\u0070ple";
	wchar_t dest[20];
	
	wcscpy(dest,src);
	wcout << L"After copying, dest = " << dest;
	
	return 0;
}

When you run the program, the output will be:

After copying, dest = Ăpple