Why do I get warning : ISO C++ forbids converting a string constant to 'char*' when I assign a char * to a string literal? Can't I assign string literals to char * like in the C programming language? Also, I don't get the warning when I assign string literals to const char * and the book I'm currently reading (C++ primer) talks about string literals and const char * interchangebly. Why is that?
Asked
Active
Viewed 43 times
0
KnowledgeSeeker
- 57
- 6
-
Of course you can do it in C, however C++ is not C. It's a completely different language, and you can't do it in C++ because, well, you can't. C++ does not work this way. – Sam Varshavchik Jan 12 '21 at 13:06
-
The compiler warns you when you assign a string literal to a `char*`, but not when you assign a string literal to a `const char*`. That's because the first isn't legal in C++. The compiler writers really aren't doing you a favor by only giving a warning, although that's allowed by the standard. – Pete Becker Jan 12 '21 at 15:03