linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* pointer initialization of string constant
@ 2005-01-23  6:26 Krishna Mohan
  2005-01-23 12:45 ` Progga
  0 siblings, 1 reply; 7+ messages in thread
From: Krishna Mohan @ 2005-01-23  6:26 UTC (permalink / raw)
  To: LinuxC

Dear All, 
Plz see the following code. It gives segmentation fault. ofcourse, it's
right as char *ptr = "abc" sets pointer to the address of the "abc" 
string (which may be stored in read-only memory and thus unchangeable).
My doubt : when i run gdb and execute p ptr either after or before
printf, pbc has been displayed. I'm getting segmentation fault only at
the end, it seems, because when i run ./a.out  "pbc is not displayed"
but prompted segmentation fault error. why is it so? How does
segmentation fault raises, though pointer-pointing contents has been
changed & even displayed using gdb. Will U plz explain in detail
(internals).

main()
{
        char *ptr = "abc";
        ptr[0] = 'p';
        printf("%s\n",ptr);
}

Thanx in advance. 
Krishna Mohan


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2005-01-25 14:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-01-23  6:26 pointer initialization of string constant Krishna Mohan
2005-01-23 12:45 ` Progga
2005-01-23 13:36   ` Krishna Mohan
2005-01-24  5:49     ` how to implement tail -n Venkatesh Joshi
2005-01-24  6:30       ` Eric Bambach
2005-01-25 14:10         ` Robert Lorentz
2005-01-24 19:26       ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).