Literal Strings in C ( was Re: example of how toupper() works )

Karl Heuer karl at haddock.ima.isc.com
Fri Oct 19 08:04:49 AEST 1990


In article <256 at dcsun21.dataco.UUCP> amodeo at dcsun03.UUCP (Roy Amodeo,DC ) writes:
>>	char *duh = "Hello"; ... *duh = ...
>
>In the above segment of code, the literal string pointed to by 'duh' is
>being modified in place. Is this portable according to the ANSI standard?

No.  String literals may be shared and read-only.  (K&R 1 explicitly said
otherwise, which was a botch; I choose to interpret it as having described a
particular implementation rather than the language itself.)

>What does current practice dictate on this?

Some compilers make them writable and separate; others make them read-only and
shared.  The latter compilers usually have an option to yield the former
behavior, since some existing code depends on it.

Karl W. Z. Heuer (karl at ima.isc.com or uunet!ima!karl), The Walking Lint



More information about the Comp.lang.c mailing list