Home>

If i try to assign a string literal to a TCHAR * type, the types will not match and you will get an error message.
I don't know the cause because it should be written correctly.
Thank you

· An entity of type "TCHAR *" cannot be initialized with a value of type "const char *"
-'Initializing': Cannot convert'const char [3]' to'TCHAR *'.
Corresponding source code
  TCHAR * c = _T ("A");// The above error message
What I tried

Change the character set to multibyte characters,
I added/Zc: strictStrings- to the command line but it didn't work

Supplementary information (FW/tool version, etc.)

Microsoft Visual C ++ 2019 Version 16.8.2

  • Answer # 1

    In c ++, "XXX" is a string constant.
    When referencing with a pointer, a warning error will occur if const is not added.
    In g ++, the following error occurs.

    char * p = "lll";
    t.cpp: 5:15: warning: ISO C ++ forbids converting a string constant to ‘char *’ [-Wwrite-strings]
        5 | char * p = "lll";
          | ^ ~~~~

    If you define it as a character array, it doesn't have to be a constant.
    You don't need const anymore.

     char p [] = "lll";// The string is copied to this array. You don't need const because it can be changed.

  • Answer # 2

    Was self resolved
    TCHAR c [3] = _T ("A");
    I rewrote it to and it passed for some reason
    But I don't understand the reason
    I hope you can explain this phenomenon.
    Thank you for your continued support.

  • Answer # 3

    TCHAR c [3] = _T ("A");


    I managed to rewrite it and it passed
    But I don't understand the reason
    I hope you can explain this phenomenon.
    Thank you for your continued support.