Re: Are string literals _always_ allocated in the RO section?
Jan-Benedict Glaw <[email protected]> Mon, 17 Jan 2005 18:18:53 +0100
| Newsgroups | org.kernel.vger.linux-gcc |
|---|---|
| Message-ID | <[email protected]> |
On Sun, 2005-01-16 00:18:20 +0100, Gabriel Dos Reis <[email protected]> wrote in message <[email protected]>: > Denis Zaitsev <[email protected]> writes: > > | This program: > | > | void x(char *s) > | { > | *s= 'x'; > | } > | > | main() > | { > | x("y"); > | } > | not <const char*> does nothing in this case. Should it be considered > | as a bug? > > In the program? Yes. Maybe you'd tell him more about the magic :) char *mytext = "hello"; results in a pointer (which you may later on make pointing to a different location) that points to a static, read-only string containing "hello\0". However, if you need to change the text, you need to do it like this: char mytext[] = "hello"; Cf. section 6.4.5.6 of the C99 standard. MfG, JBG -- Jan-Benedict Glaw [email protected] . +49-172-7608481 _ O _ "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
signature.asc
(application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) iD8DBQFB6/N9Hb1edYOZ4bsRAlauAJ9XtSSleihH5ZDpy9dp3hlXJGNuVwCfSRej az2UwUdGYExvYgA99nv7DLQ= =AXV3 -----END PGP SIGNATURE-----