Compiler incompatibility

I wrote a Visual C++ program which in an .h file had the lines

char*charnames[256]={"00",
"lbutton",
"rbutton",
"cancel",
"mbutton",
....etc....

That always compiled OK under my old Visual C++ compiler. But the new compiler rejects it with "a value of type "const char*" cannot be used to initialize an entity of type "char*".

That fault also happens with the lines
#define PROGNAME "makeobj"
#define TEXPROGNAME "makeobj texturemap"

char*progname = PROGNAME, *texprogname = TEXPROGNAME;

I had other faults with objects of the mode char* used in an = assignment or as a function call parameter, which my old compiler accepted and my new compiler rejects. Please, is there a list of these changes in what the compiler accepts and what it rejects? Is there a workaround for these changes?
Last edited on
Yes, make them all const char *
"strings" were made constants in C about 30 years ago, so there's really no excuse at all for being tardy.
> is there a list of these changes?

Annex C of the standard 'Compatibity' lists the major changes across versions.
http://eel.is/c++draft/diff


> Is there a workaround for these changes?

For this specific change, C.6.2/4 http://eel.is/c++draft/diff.lex#4 has

Change: String literals made const.
The type of a string-literal is changed from “array of char” to “array of const char”. ...

Effect on original feature: Change to semantics of well-defined feature.

Difficulty of converting: Syntactic transformation. The fix is to add a cast:
1
2
3
4
5
6
char* p = "abc";                // valid in C, invalid in C++
void f(char*) {
  char* p = (char*)"abc";       // OK: cast added
  f(p);
  f((char*)"def");              // OK: cast added
}

Thanks.

In this new version, how can I tell the compiler to treat all 'char*' declarations as 'long char*'?
Last edited on
?? long char* ??

long char* isn't a valid type. Long means long integer and char* is a pointer to memory that contains data of type char.

Do you mean a cast from char* to long?

How are you using this? Can you give an example code?

Note that C++ specification has been tightened over the years and some 'flaky' code that might have compiled and run Ok years ago no probably won't.
Last edited on
The offending statement is

GetDlgItemText(db, 11, C, 1024);


where GetDlgItemText is automatically defined as GetDlgItemTextW

and C is a local variable char C[1024];
Last edited on
The compiler settings are set to use wide-char as default (hence the W). If you aren't using wide-chars in your program, the easiest way is to change the compiler settings.

Project/properties/Advanced/Character Set. Change to Use Multi-Byte Character Set

Alternatively, you can specifiy that the ASCII versions of the API's are used by specifiying an A at the end of the name (instead of the W)

Note that there are probably other settings that should/need to be changed - ie general/c++ language standard. It defaults to c++14. set to either C++20 or preview if you want to also use some features from C++23.


Last edited on
Topic archived. No new replies allowed.