From mboxrd@z Thu Jan 1 00:00:00 1970 To: 9fans@cse.psu.edu From: Richard Bos Message-ID: <404c460f.500459755@news.individual.net> References: Subject: [9fans] Re: constant string Date: Mon, 8 Mar 2004 10:53:36 +0000 Topicbox-Message-UUID: 2323ab88-eacd-11e9-9e20-41e7f4b1d025 Prem Mallappa wrote: > #include > > int main(void) > { > char *s = "Hello"; > > s[2] = 'z'; > > printf ("%s\n", s); > > return 0; > } > my question is when i compile this in Linux (gcc) i get a segmentation > fault at the second statement of main(), > As far as my knowledge i think this is because in 2nd chapter of K&R book > it is being mentioned that > ' any thing enclosed between " and " is a string constant' so here i am > changing a constant, and i get a segmentation fault ( and i also noticed > that gcc stores the string Hello in read-only datasegment) . Basically, yes, you're correct. > but when i compile the same thing in plan9 C compiler ( both native "8c" > and "pcc" i get a output of "Hezlo") > why this is happenning.. To begin with, IIRC Plan 9 C isn't exactly ISO C. But in any case, assigning to a constant (in those cases where you don't get a mandatory warning, and when you ignore the warning and do the assignment anyway, as well) invokes undefined behaviour. This means that the implementation is free to behave as it chooses to; this includes crashing, behaving as if it were correct code, and anything in between. Richard