As per book The C Programming Language, 4th Edition -
In C and in older C code, you could assign a string literal to a non-const char*:
void f() { char* p = "Plato"; // error, but accepted in pre-C 11-standard code p[4] = 'e'; // error : assignment to const }
It would obviously be unsafe to accept that assignment. It was (and is) a source of subtle errors, so please don’t grumble too much if some old code fails to compile for this reason.
It suggest that, above code should give error, but I am getting a warning instead.
21:22:38 **** Incremental Build of configuration Debug for project study ****
Info: Internal Builder is used for build
g -std=c 0x -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\study.o" "..\\src\\study.cpp"
..\src\study.cpp: In function 'void f()':
..\src\study.cpp:10:13: warning: ISO C forbids converting a string constant to 'char*' [-Wwrite-strings]
CodePudding user response:
The GCC compiler is somewhat permissive by default and allows some extension such as implicitly converting away the constness of a string.
Most likely this extension was added to keep compatibility with C.
To disable those extensions, simply add the --pedantic-errors
flag that will make the compiler refuse invalid code.