Re: A question on string literals



>> 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.
.



Relevant Pages

  • Lexical contexts; or, Lambda, the Ultimate Module?
    ... (defun foo (...) ... Here the `defcontext' declares, not a lexical environment, but a ... that binds the names declared in the context to the values in the ... certainly slow down incremental compilation a bit. ...
    (comp.lang.lisp)
  • Re: [ANN] test/spec 0.10, a BDD interface for Test::Unit
    ... context "Foo" do ... specify "should bar" do ... When we put a cursor inside a def test_case and claw, ... remap of test_case architecture is the ability to run nested context{} blocks with incrementally nested setupblocks. ...
    (comp.lang.ruby)
  • Re: Selecting directories containing specific fileglobs
    ... glob() ) in a scalar context has weird magical ... Actully the second prints the first two starting foo because the scalar ... context glob function remembers that it's got a list of files starting ... You need to force a list context on glob. ...
    (comp.lang.perl.misc)
  • Re: Perl vs Python vs Ruby
    ... I think it makes perfect sense in the context of Perl. ... > and that being able to use %foo, @foo, ... > Ruby is closely related to Smalltalk and Lisp. ...
    (comp.lang.perl.misc)
  • Re: "instance_eval" (eg, sent to a class object)
    ... I do know what it means to evaluate code in the context of a *class* ... # We're now in the context of the object Foo. ... Intro to Rails, London, UK, December 3-6 ...
    (comp.lang.ruby)