I'm recently starting programing C Windows API from Charles Petzold, and somewhere we must write a header contain a lot of similar lines like below:
SM_CXSCREEN, TEXT ("SM_CXSCREEN"),
TEXT ("Screen width in pixels"),
and after use this header in a program, the GCC warning me like this:
ISO C++ forbids converting a string constant to 'TCHAR*' {aka 'char*'} [-Wwrite-strings]gcc
I hope to understand more about this specific warning.
The error "ISO C++ forbids converting a string constant to TCHAR*
{aka char*
}" occurs because in C++, string literals like "example"
are of type const char[]
(or const wchar_t[]
for wide-character literals like L"example"
). However, you are trying to assign or pass them to a non-const pointer type like TCHAR*
(or char*
).
This is dangerous because modifying a string literal is undefined behaviour - thus warning