difference between *y++ and ++*y?

13,818

Solution 1

The expression x = *y++ is in effects same as:

x = *y;
y = y + 1;

And if expression is just *y++; (without assignment) then its nothing but same as y++;, that is y start pointing to next location after increment.

Second expression ++*y means to increment the value pointed by y that same as: *y = *y + 1; (pointer not incremented) It will be better clear with answer to your first question:

Suppose your code is:

int x = 30, *y;
int temp;
y = &x;

temp = *y++; //this is same as:  temp = *y;  y = y + 1;

First *y will be assigned to temp variable; hence temp assigned 30, then value of y increments by one and it start point to next location after location of x (where really no variable is present).

Next case: Suppose your code is:

int x = 30, *y;
int temp;
y = &x;

temp = ++*y;  //this is same as *y = *y + 1; temp = *y;

First value of *y increments from 30 to 31 and then 31 is assigned to temp (note: x is now 31).

next part of your question (read comments):

int x = 30, *y, *z;

y = &x;       // y ---> x , y points to x
z = y;        // z ---> x , z points to x
*y++ = *z++;  // *y = *z, y++, z++ , that is 
              // x = x, y++, z++
x++;          // increment x to 31

Solution 2

what is the difference between *y++ and ++*y?

In case of expression *y++ and *z++; because the postfix version ++ takes precedence over *, the compiler sees this as;

*(y++) = *(z++);

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;

Expression                 Meaning
-------------------------------------------------------------------------------------
*y++ or *(y++)         Value of expression is *y before increment; increment y latter
(*y)++                 Value of expression is *y before increment; increment *t later
*++y or *(++y)         Increment y first; value of expression is *y after increment
++*y or ++(*y)         Increment *y first; value of expression is *y after increment

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.

Solution 3

what is the difference between *y++ and ++*y?

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 that y 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

q = *y++ 

then the side effect of the ++ could happen at any point. This could be:

q = *y;
y = y + 1;

or it could be treated as

t = y;
y = y + 1;
q = *t;

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:

q = ++*y

could be treated as:

t = *y + 1;
*y = t;
q = t;

or as

t = *y + 1;
q = t;
*y = t;

Remember, C does not produce very many restrictions on the order in which side effects may happen, so be careful.

Solution 4

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.

Share:
13,818
Ravi Bisla
Author by

Ravi Bisla

Doing computer engineering[2010-2014] from VIT University (Vellore), India. Email: [email protected] Blog: http://ravibisla.wordpress.com

Updated on August 13, 2022

Comments

  • Ravi Bisla
    Ravi Bisla over 1 year

    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;
    }