Possible Duplicate:
Modifying C string constants?
As far as I understand if you want to make a c string constant you should give a declaration like :
const char* str = "hello";
or
char* const str = "hello";
But a simple declaration like:
char* str = "hello"
If I try to modify the string by any means - like
str[2] = 'f';
or
*(str+2) = 'f';
I always get a segmentation fault, why is that ?
Also if I do a declaration like
const char* const str = "hello";
str = "bye";
as per scott meyers I am trying to modify a const pointer and should get warning, error or segfault, but I get nothing and code compiles and executes perfectly. I am puzzled, I am using g++ on ubuntu 11.10.