Sri Ragaventhirar wrote:
(several times. DON'T DO THAT. We are not a chatgroup.)
char *s = "Google"
Where this "Google" will be stored?
Somewhere that lasts from when the program starts to when it
ends. (Maybe even longer, but you can't tell.)
Why it can't be changed?
You don't know if it can or can't be. All you know is that
trying to change it gets undefined behaviour: in other words,
the implementation can do whatever it likes.
like s[1]='h'; -this is giving error
please explain this.
What kind of error? A compile-time error or a run-time one?
The compiler may choose to store the string in a region of
read-only memory. That memory may cause an signal to be raised
if you try and change it. BOOM.
The compiler may choose to store the string in a region of
read-only memory. That memory may ignore any attempt to change
it.
The compiler may choose to store the string in a region of
writeable memory. Assigning to elements of the string will
update it.
The compiler may choose to store the string in a region of
read-only memory. Writing to that memory may cause a
hedgehog to come and sign in your ear. Badly.
All four of these are legitimate implementations - even the
fourth, although the shortage of singing hedgehogs makes
it less likely you'll encounter it.
So don't assign into string literals.
--
Chris "We are BROKE-en" Dollin
"We did not have time to find out everything we wanted to know."
- James Blish, /A Clash of Cymbals/