[comp.lang.c] should string literals be const?

karl@haddock.ISC.COM (Karl Heuer) (08/09/88)

In article <12891@mimsy.UUCP> chris@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@haddock.isc.com), The Walking Lint
Followups to comp.std.c.