0

I need to make and WCHAR. But it wont work, and i always get an error:

Error   C2440   'initializing': cannot convert from 'const wchar_t [11]' to 'WCHAR *'
 StateError (active)    E0144   a value of type "const wchar_t *" cannot be used to initialize an entity of type "WCHAR *

My code:

WCHAR *Testlooll = L"TEST";
Bequiny
  • 41
  • 1
  • 5

2 Answers2

1

L"TEST" is a string literal of type const wchar_t[5], which is an array of const characters (since the literal exists in read-only memory). You are trying to initialize a WCHAR*, which is a pointer to a non-const character, to point at that array.

Initializing a pointer to non-const character data to point at an array of const character data is deprecated in C++98 (to maintain backwards compatibility with legacy code), and is illegal in C++11 onwards.

You need to change the declaration of Testlooll according:

const WCHAR *Testlooll = L"TEST";

Or:

LPCWSTR Testlooll = L"TEST";
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

In addition to Remy Lebeau's answer, if for some reason you can't modify the defination of Testlooll. You can just cast the const arry to a wchar_t*. For example,

struct someLibaryType
{
    WCHAR *Testlooll
};

someLibaryType a;
a.Testlooll = (wchar_t*)L"TEST";

Someone maybe argue should cast to WCHAR* just keep same with the defination type of Testlooll. But in this context, you've already used L to identify a string, so it has to be wchar_t*.

Zhang
  • 3,030
  • 2
  • 14
  • 31