question about "++"

M

mdh

mdh wrote:
Thanks Bart for that nice explanation and thanks all for the help in trying to understand this. It really did take time to understand the concept of "ordering", or more specifically, the lack thereof to the compiler. The explanation of sequence points finally cemented this for me. (I am not sure if "2" thank-you s will appear as once again, Google is on the Fritz....which seems to be happening just about weekly now. )
thanks all again.
 
B

Barry Schwarz

Beware. *s++ and (*s)++ mean different things.

int zog[2] = { 6, 42 };
int *s;

s = zog; *s++ = 12;
/* zog is now { 12, 42 }, *s = 42 */


At the risk of showing total ignorance...( except by now I suppose a
lot of people who answer my queries are used to this !!! :))

So, s is initialized to point at the address of zog[0]. On the face of
it, 12 is assigned to zog[0], then the pointer is incremented by one
and dereferenced. This makes sense, but then I looked at P53 of K&R
and am confused. If I read this correctly, the assignment should occur
last, as it is the lowest order of precedence. Then if * and ++ are
of equal precedence ( line 2, same page) and the associativity is
right to left, why am I incorrect ( and I must be ) in saying that *s+
+ = 12 should first increment the pointer, then dereference it, then
assign 12 to that index, and give zog { 6, 12} /** wrong **/ but why?
I will leave the others for now, as I am sure the answer to this will
help with those examples.

You need to distinguish between the evaluation of an operator (or
perhaps more precisely the expression the operator is part of) and any
side effect the operator may have.

The ++ operator always evaluates to the original value of its operand.

As a side effect, it increments the value of the operand. The only
thing you know about the timing of this side effect is that it will be
completed at or before the next sequence point.

For your example:

*s++ associates right to left and therefore is parsed as
*(s++). We know that s++ evaluates to &zog[0]. We also know that s
will eventually be incremented to &zog[1] but that has nothing to with
the evaluation of the expression s++.

The * operator will be applied to the evaluated expression (it
is completely unaffected by the side effect) and will result in the
object zog[0]. Therefore the left operand of the = operator is
zog[0].

The actual incrementing of s can occur any time the compiler
feels like it as long as the resulting executable code evaluates the
expression correctly.


Remove del for email
 
M

mdh

You need to distinguish between the evaluation of an operator ....... and any
side effect....

The ++ operator always evaluates to the original value of its operand.

As a side effect, it increments the value of the operand.......
completed at or before the next sequence point.

For your example:

*s++ associates right to left and therefore is parsed as
*(s++). We know that s++ evaluates to &zog[0]. We also know that s
will eventually be incremented to &zog[1] but that has nothing to with
the evaluation of the expression s++.

The * operator will be applied to the evaluated expression (it
is completely unaffected by the side effect) and will result in the
object zog[0]. Therefore the left operand of the = operator is
zog[0].

And this was certainly a large part of the confusion for me.

And, as you say in the next paragraph, understanding that "timing"
plays no role, is crucial, and has been nicely pointed out, by you
here.
The actual incrementing of s can occur any time the compiler
feels like it as long as the resulting executable code evaluates the
expression correctly.


Thank you Barry. I think this, as RH explained, once understood,
really does take one to the next level of understanding of C. I am
just beginning to wonder how many levels there are? :)
 
B

Bart van Ingen Schenau

mdh said:
Thank you Barry. I think this, as RH explained, once understood,
really does take one to the next level of understanding of C. I am
just beginning to wonder how many levels there are? :)

There always seems to be at least one level more that you have not
reached yet.
In contrast, in C++ there always seem to be at least 10 levels more :)

Bart v Ingen Schenau
 
J

Johan Bengtsson

Bart said:
There always seems to be at least one level more that you have not
reached yet.
In contrast, in C++ there always seem to be at least 10 levels more :)
Winning IOCCC (or even making an entry) is one personal goal of mine and
lots of level up (I am not that far away - from making an entry...)
Perhaps not a recommendable goal in life and not a recommended level to
reach for "normal" programming but anyway...
 

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

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top