[net.lang.c] Read-only Strings, and the

kpmartin@watmath.UUCP (Kevin Martin) (10/20/84)

For strings being read-only by default, Yes, please!
Unfortunately, this is the sort of decision that can't be justified with
reasons

I don't really like the idea of the funny type cast, where
    (const) "abc"
gives a string in read-only storage. I dislike it because it is indeed
a funny type cast, in that the operand of the cast is affected, sort of.
The string "abc" is an array of char, but an array isn't an rvalue,
and type casts operate on rvalues. Taking the rvalue of an array
implicitly takes the address of the first element, so normally, the
above would be casting the (already constant) pointer to the 'a' into
a constant pointer to the 'a'. So far, so good. But this particular cast
has the strange side effect of making the 'a' itself (as well as the
"bc") read-only, rather than just affecting the pointer.

For the rare cases where a writeable string is wanted, it can
be made into a static initialized variable. This would also make it
obvious to someone reading the code that the program intends to alter the
string.
                    Kevin Martin, UofW Software Development Group

henry@utzoo.UUCP (Henry Spencer) (10/26/84)

> I don't really like the idea of the funny type cast, where
>     (const) "abc"
> gives a string in read-only storage. ...

Actually, it's worse than that, although your major point ("having the
cast affect the thing pointed to as well as the pointer is bizarre")
is valid.  It's worse because the above is not a legal C fragment; it
would have to be

	(const char *) "abc"

Bletch.  Down with writeable strings!
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry