Article View: comp.std.c
Article #54Re: on "use", and the lvalue-ness of string literals
From: Blair P. Houghton
Date: Mon, 02 Nov 1992 20:04
Date: Mon, 02 Nov 1992 20:04
40 lines
1901 bytes
1901 bytes
In article <27143@dog.ee.lbl.gov> torek@horse.ee.lbl.gov (Chris Torek) writes:
>In article <1992Nov2.162310.10094@ichips.intel.com> bhoughto@sedona.intel.com
>(Blair P. Houghton) writes:
>> "A string literal is a primary expression.
>> It is an lvalue..."
>> "foo" = "bar"; /* Is this okay? */
>
>No, it is a `non-modifiable lvalue', just like an array---and in fact,
>a string literal *is* an array (type `array N of char', even though the
>actual char's may themselves be const).
The Standard says something about translation phase 7 and
adding a '\0' and initializing an array with static storage
duration.
It doesn't say directly that "foo" has array type (in the
context above), and there are more than one interpretation
for "bar" (here), at least one of which is to attempt to
initialize location "foo" with `{ 'b', 'a', 'r' }'.
So we say,
"If the program attempts to modify a string literal
... the behavior is undefined."
X3.159-1989, sec. 3.1.4,
String Literals, p. 32, ll. 8-9)
and my example is moot, and we learn that the type of a
string literal is equivalent to (compatible with?)
<const array N of const char>. Even though the first
`const' is redundant, the second is now definite.
E.g.,
"foo"[2] = 'g';
results in undefined behavior.
--Blair
"Whew. Glad that's over..."
Technical Details
Message-ID:
<1992Nov2.200406.18152@ichips.intel.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!ferkel.ucsb.edu!taco!gatech!destroyer!sol.ctr.columbia.edu!eff!iWarp.intel.com|ichips!sedona!bhoughtoReferences:
<1992Nov2.162310.10094@ichips.intel.com> <27143@dog.ee.lbl.gov>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c Path: arc1-ca.pugleaf.net!sparky!uunet!ferkel.ucsb.edu!taco!gatech!destroyer!sol.ctr.columbia.edu!eff!iWarp.intel.com|ichips!sedona!bhoughto From: bhoughto@sedona.intel.com (Blair P. Houghton) Subject: Re: on "use", and the lvalue-ness of string literals Message-ID: <1992Nov2.200406.18152@ichips.intel.com> Sender: news@ichips.intel.com (News Account) Organization: Intel Corp., Chandler, Arizona References: <1992Nov2.162310.10094@ichips.intel.com> <27143@dog.ee.lbl.gov> Date: Mon, 2 Nov 1992 20:04:06 GMT Lines: 40