> Didn't work for me, it gives a " invalid conversion from `const char*' to `char*'" compile error.
> Would it be correct to say that char *arr = "123"; is like this to the compiler:
The ability to do char *arr = "a string"; is a special case relaxation of the rules. In other situations, the compiler will tell you whether you're breaking 'const-correctness'.
Some time ago, a change was added to the standards to allow"string" constants to be placed in read-only memory (and thus const). But they don't have to be. Another thing the standards people try to do is avoid breaking existing common practice in existing code.
Any new code, and any code being maintained should be writing
const char *arr = "a string";
For the moment, you can get away with it, but you should really start using const to be future-proof.
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Offline 7,164 posts
since Dec 2005