There is a line of code like this:char* arr="Hello"
. I have heard that the string Hello
is located at someplace for constant things like the value of a const variable (is that right?) . My confusion is, if that assumption is true, why can we let a pointer of type char*
point to something that is const
, giving people opportunity to change the const "Hello" string. I mean, isn't it unreasonable for a language like C which have type-check to allowed this happen?
Asked
Active
Viewed 206 times
0

walkerlala
- 1,599
- 1
- 19
- 32
1 Answers
0
See, the "Hello"
here is a string literal, but it's not const
, by standard.
Most compilers, including gcc
, puts the string literals in read only memory. Standard only mandates not to modify the string literals.
Quoting from C11
standard, chapter ยง6.4.5, String literals
If the program attempts to modify such an array, the behavior is undefined.

Sourav Ghosh
- 133,132
- 16
- 183
- 261