Modifying wchar_t

Hey guys,

Im attempting to modify a wchar_t but i get an access violation error. For example:
1
2
3
4
5
6

wchar_t* str = new wchar_t[30];
str = L"Hello!";

str[2] = L'A'; // Access violation error?


Why is this happening?
Line 3 creates a memory leak. You are reassigning the pointer from something you newd to a const wchar area, so when you try to modify it, you can't. You need to use a strcpy() like function (I don't know if there are ones for wchar_t though).
Windows provides them.
1
2
3
4
#include <windows.h>
...
wchar_t* str = new wchar_t[30];
lstrcpyW( str, L"Hello!" );

Hope this helps.
Topic archived. No new replies allowed.