Re: A question on string literals
- From: Chris Torek <nospam@xxxxxxxxx>
- Date: 15 Aug 2005 19:42:59 GMT
>> Richard Heathfield wrote:
>>>>> [Array-to-pointer] decay happens only in value contexts,
>pete wrote:
>> Where are you getting that from?
In article <ddopvu$f6k$1@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
Richard Heathfield <sorry.spammers@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>Well, I must admit I was getting it from Chris Torek's constant incantations
>of "The Rule", and perhaps I was over-interpreting it. The fact remains
>that the conversion you expect is not guaranteed by the Standard for the
>simple reason that the code itself violates a constraint.
Indeed, "The Rule" and the C standards (both of them) are not
literally the same -- but they *are* isomorphic, i.e., both give
the same result. I think my formulation is easier to understand,
as well, because we need the idea of "value context" vs "object
context" in the first place, so that we can figure why:
int x, b;
...
x = b;
copies b's *value* to the *object* named x.
If x used to be 3 before the assignment, and b is 7, why does this
set x to 7, instead of setting b to 3, or setting 3 to 7, or setting
7 to 3? (Of course, only two of those four possibilities even make
sense.) The answer is that the "=" operator demands an object on
its left, and a value on its right. The context on the left of
the "=" is an "object context", and the context on the right is a
"value context". Put down a value when something needs a value
and there is no problem:
x = 3;
Here x is an object and 3 is a value (of the correct type), so this
sets x to 3. Name an object on the right, though, and the operation
automatically fetches the object's value:
x = b;
so if b was 7, x becomes 7 too.
Once you understand the idea of "object context" and "value context",
you simply have to memorize which operators have which context(s):
&foo - object context
sizeof foo - object (or maybe even "sizeof") context
foo + bar - two value contexts
foo = bar - one object context, one value context
++foo - object context
and so on.
Given all of that, applying The Rule becomes easy, and whenever
the result is not "this makes no sense and a diagnostic is required",
it gives the same result as the more complicated way the C standards
put it.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
.
- Follow-Ups:
- Re: A question on string literals
- From: Joe Wright
- Re: A question on string literals
- From: Dave Vandervies
- Re: A question on string literals
- From: Tim Rentsch
- Re: A question on string literals
- References:
- A question on string literals
- From: junky_fellow
- Re: A question on string literals
- From: Richard Heathfield
- Re: A question on string literals
- From: pete
- Re: A question on string literals
- From: Richard Heathfield
- A question on string literals
- Prev by Date: Re: word count
- Next by Date: AND and OR and parentheses
- Previous by thread: Re: A question on string literals
- Next by thread: Re: A question on string literals
- Index(es):
Relevant Pages
|