Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What's the reason of GCC warnings for converting an string constant to 'TCHAR *'?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I hope to understand more about this specific warning.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading