Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: why could not change the value of char* |
| Date | 2014-03-04 15:18 -0500 |
| Organization | Self |
| Message-ID | <53163503.8000107@verizon.net> (permalink) |
| References | <ca781f3b-4de1-41ad-ae1c-71cc3cc5092b@googlegroups.com> <lf5ajg$r57$1@blue-new.rahul.net> |
On 03/04/2014 02:46 PM, Edward A. Falk wrote: > In article <ca781f3b-4de1-41ad-ae1c-71cc3cc5092b@googlegroups.com>, > jack jack <qzqsmile@gmail.com> wrote: >> recently i learn the pointer of c. wrote a programme below >> >> 5 char *s = "Hello world!"; >> 7 *s = 'h'; > > Any modern compiler/linker will treat the string "Hello world!" as a > constant and put it into read-only memory. Attempting to over-write the > first byte will probably cause the system to throw a wobbly. > > Frankly, I don't even know why the compiler lets you assign this string > to a non-const variable, except perhaps that too much legacy code would > break if the compiler forbade it. A pointer to the string was used to initialize 's'; the only assignment in that code was of a character, not a character string. The fact that the compiler allowed the initialization might have to do with the fact that the it doesn't violate any of C's rules. The assignment of 'h' to *s violates 6.4.5p7, but that only specifies that the behavior is undefined, no diagnostic is required. The reason the initialization doesn't violate any rules is because "Hello world!" has the type char[13], whereas it should properly have the type "const char[13]". That decision was indeed made because 'const' was a relatively late addition to the language, and by the time it was added, there was indeed too much legacy code that would break. However, that decision was made by the C committee, not by the implementor of that particular compiler.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
why could not change the value of char* jack jack <qzqsmile@gmail.com> - 2014-03-01 00:12 -0800
Re: why could not change the value of char* Ian Collins <ian-news@hotmail.com> - 2014-03-01 21:16 +1300
Re: why could not change the value of char* Kaz Kylheku <kaz@kylheku.com> - 2014-03-01 08:42 +0000
Re: why could not change the value of char* jack jack <qzqsmile@gmail.com> - 2014-03-01 01:03 -0800
Re: why could not change the value of char* falk@rahul.net (Edward A. Falk) - 2014-03-04 19:46 +0000
Re: why could not change the value of char* Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-04 15:09 -0500
Re: why could not change the value of char* James Kuyper <jameskuyper@verizon.net> - 2014-03-04 15:18 -0500
Re: why could not change the value of char* John Bode <jfbode1029@gmail.com> - 2014-03-07 11:14 -0800
Re: why could not change the value of char* gazelle@shell.xmission.com (Kenny McCormack) - 2014-03-07 19:29 +0000
Re: why could not change the value of char* Ken Brody <kenbrody@spamcop.net> - 2014-03-07 14:55 -0500
Re: why could not change the value of char* glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-07 20:46 +0000
Re: why could not change the value of char* James Kuyper <jameskuyper@verizon.net> - 2014-03-07 16:12 -0500
Re: why could not change the value of char* glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-07 21:49 +0000
Re: why could not change the value of char* Kaz Kylheku <kaz@kylheku.com> - 2014-03-07 22:57 +0000
Re: why could not change the value of char* anish kumar <yesanishhere@gmail.com> - 2014-03-07 15:05 -0800
Re: why could not change the value of char* Ian Collins <ian-news@hotmail.com> - 2014-03-08 14:22 +1300
Re: why could not change the value of char* Ken Brody <kenbrody@spamcop.net> - 2014-03-07 14:43 -0500
Re: why could not change the value of char* gazelle@shell.xmission.com (Kenny McCormack) - 2014-03-07 19:52 +0000
csiph-web