Why do I get a compiler warning for converting a string literal to a char*, is it bad?

后端 未结 2 1833
别那么骄傲
别那么骄傲 2021-01-21 08:04

So the compiler tells me this is a deprecated conversion from a string-literal to char*:

 char* myString = \"i like declaring strings like this\";
2条回答
  •  离开以前
    2021-01-21 08:31

    It shouldn't even compile. If you need to pass it to function that you are sure won't change the string you need to use const cast, its one of its correct uses:

    functionName(const_cast("something"));
    

    Or if you don't want the const cast, you can copy the string to the stack:

    char str[] = "something";
    functionName(str);
    

提交回复
热议问题