I'm confused in how this code will get executed. Suppose we have
int x=30,*y,*z;
y=&x;
what is the difference between *y++ and ++*y? and also what will be the output of this program?
#include<stdio.h>
int main(){
int x=30,*y,*z;
y=&x;
z=y;
*y++=*z++;
x++;
printf("%d %d %d ",x,y,z);
return 0;
}
The expression
x = *y++
is in effects same as:And if expression is just
*y++;
(without assignment) then its nothing but same asy++;
, that isy
start pointing to next location after increment.Second expression
++*y
means to increment the value pointed byy
that same as:*y = *y + 1;
(pointer not incremented) It will be better clear with answer to your first question:Suppose your code is:
First
*y
will be assigned totemp
variable; hencetemp
assigned30
, then value ofy
increments by one and it start point to next location after location ofx
(where really no variable is present).Next case: Suppose your code is:
First value of
*y
increments from30
to31
and then31
is assigned totemp
(note:x
is now31
).next part of your question (read comments):
I trust that you understand what the operators ++ and * means when used separately. When used together then operator precedence comes into play. In C++ the ++ operator has a higher precedence than the * operator. So effectively
*y++
means*(y++)
and++y*
means(++y)*
. I hope this helps.In case of expression
*y++
and*z++
; because the postfix version++
takes precedence over*
, the compiler sees this as;In case of
++*y
; compiler sees this as++(*p)
and it will first increment the value of the object it points to (x
in this case) and then return its incremented value.Summary table for other possibilities;
EDIT: As pointed out by Eric Lippert in his comment that saying: value of expression is
*y
before increment, increment y later is misleading, I want to clarify here that the words I used latter and after to emphasize that previous or next value of*y
, respectively, will be used in expressions.Note that, the side-effect can be produced in any order, either side-effect produce first and value assigned latter or value assigned first and side-effect produce latter. For more detail read the answers :-- 1, 2 given by Eric Lippert.
The meaning of an expression in C is characterized by two things: what value it produces and what side effects it produces.
Let's examine the first expression.
Postfix increment is of higher priority than dereferencing, so this is
*(y++)
.The postfix increment produces a side effect: it changes the value of
y
to point to a different location. The postfix increment also produces a value: the value thaty
had before it was incremented. The*
operator then dereferences that value to produce an lvalue: that is, something you can use as a variable, either to store to or to fetch from.I note that the side effect can happen at any point before or after the dereferencing. If you said
then the side effect of the
++
could happen at any point. This could be:or it could be treated as
Both are perfectly legal. (Except of course that if
y
is itself an expression with side effects, those side effects must be produced only once. For clarity, I'll make that assumption throughout.)How about
++*y
? That is straightforward:*y
produces a variable, the content of the variable is incremented, and the value of the expression is the incremented value. Note that again, the side effect can be produced out-of-order:could be treated as:
or as
Remember, C does not produce very many restrictions on the order in which side effects may happen, so be careful.