question about k&r2

C

CBFalconer

Old said:
Hardly, the latter causes undefined behaviour. Without an
intervening sequence point, p is both written, and read for
a purpose other than to determine the value to be written.
(That purpose is in fact to determine the location to write
another value).

p is NOT written. p->next is. That does not affect the value of
p. Well defined.
 
K

Keith Thompson

CBFalconer said:
p is NOT written. p->next is. That does not affect the value of
p. Well defined.

Yes, p is written; it's the target of an assignment.

<SEQUENCE_POINT> p = p->next = q; <SEQUENCE_POINT>

C99 6.5p2:

Between the previous and next sequence point an object shall have
its stored value modified at most once by the evaluation of an
expression. Furthermore, the prior value shall be read only to
determine the value to be stored.

Between the two sequence points, p is modified once (when the result
of "p->next = q" is assigned to it), *and* p is read for a purpose
other than to determine the value to be stored (in the evaluation of
the lvalue p->next, to determine the location to which to assign the
value of q).
 
O

Old Wolf

p is NOT written. p->next is. That does not affect the value of
p. Well defined.

p most certainly is written; it is by itself on the left of an
assignment operator!

This code straight-out violates C99 6.5p2 (which Keith was kind
enough to quote). Ben Pfaff says there has been some debate over
this; if so then perhaps the debate should be about whether to
change the standard..?
 
C

CBFalconer

Keith said:
Yes, p is written; it's the target of an assignment.

I was referring to the interior portion of the statement. I wasn't
clear.
<SEQUENCE_POINT> p = p->next = q; <SEQUENCE_POINT>

Agreed so far
C99 6.5p2:

Between the previous and next sequence point an object shall have
its stored value modified at most once by the evaluation of an
expression. Furthermore, the prior value shall be read only to
determine the value to be stored.

Between the two sequence points, p is modified once (when the result
of "p->next = q" is assigned to it), *and* p is read for a purpose
other than to determine the value to be stored (in the evaluation of
the lvalue p->next, to determine the location to which to assign the
value of q).

And I disagree here, although I see your point. I think this
should head for the standards group, so cross-posted.

--
If you want to post a followup via groups.google.com, ensure
you quote enough for the article to make sense. Google is only
an interface to Usenet; it's not Usenet itself. Don't assume
your readers can, or ever will, see any previous articles.
More details at: <http://cfaj.freeshell.org/google/>
 
P

pete

CBFalconer said:
No, but I still disagree. The left assignment is of the value of
the statement "p->next = q".
To evaluate that statement

If (p->next = q) was a statement, then you'd be right.
To evaluate that "expression" ...
you have to evaluate the assignment "= q".

The value of the expresssion (p->next = q), is equal to (q).
There never is an assignment "p = q" in the chain.
This is because, in C, assignment statements have a value,

Statements don't have values in C.
and that statement has to be performed before that
value is available.

You're mixing your terms "statements" and "expressions"
and that's the problem.
You're seeing sequence points where there are none.
Statements are sequence points.
Assignment isn't a sequence point.


(p = p->next = q) associates like (p = (p->next = q))

The value of (p->next = q) is the same as (q)
and that's why the value of q can be assigned to p.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top