String literals to PTCHAR is deprecated

492 Views Asked by At

I am working on Legacy code where a string literal is assigned to a variable of type PTCHAR (pointer to char) which is defined in the header: typedef WCHAR TCHAR, *PTCHAR;

PTCHAR str;
str = _tcsrchr(dir, '\\');
*(str++)=0;

str = TEXT("This is stackoverflow");

I am getting a warning 'conversion from a string literal to pointer-to-character (non-const) is deprecated', I understand warning is coming because a const is assigned to a non-const pointer variable, but I can't make str CONST(LPCSTR) because it is being modified in code as *(str++)=0;

Is there a way to solve this?

1

There are 1 best solutions below

2
Brecht Sanders On

If you want to modify the string later, you will need to make a copy of it, for example using _wcsdup(). Don't forget to free() it when done.