char *s = "hello";
The code above allocates 6 bytes in read-only section of a program (I've forgot the name of the section) to store the string hello
. Then, s
is initialized to point to the first character of the string hello
. Modifying the string "hello"
is undefined behavior. Besides, "hello"
itself is constant in nature. The program doesn't have permission to change read-only section.
I'm using MS VC++ 2010 Express. My question is, why does the compiler allows s
, which is a char *
, to point to the constant string? Shouldn't there be a compiler error? Shouldn't the compiler force us to use const char *s = "hello";
instead of char *s = "hello";
?
Thanks.