should string literals be const? (was: `static')

Karl Heuer karl at haddock.ISC.COM
Tue Aug 9 04:53:30 AEST 1988


In article <12891 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>Strangely, while "string"s are allowed to be read-only, they have type `array
>of char' rather than `array of const char'.  (I think this is a mistake.)

It would certainly be more consistent.  As Dennis Ritchie said, having two
non-writable widgets (string literals and consts) without making the former a
case of the latter indicates that something was botched.

The Committee says that it's to allow old code of the form `char *p = "foo";'
to be grandfathered in without generating a diagnostic for assigning a const
to a non-const.  I'm not completely convinced by this argument.  If p is being
used in a non-const context, then there *should* be a diagnostic.  If not,
then it should logically be declared const, and programmers should be
encouraged to do so (a warning may be appropriate here).  At the very least,
X3J11 should have deprecated non-const string literals.

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint
Followups to comp.std.c.



More information about the Comp.lang.c mailing list