C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 3)
3.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    int x=30, *y, *z;
    y=&x; /* Assume address of x is 500 and integer is 4 byte size */
    z=y;
    *y++=*z++;
    x++;
    printf("x=%d, y=%d, z=%d\n", x, y, z);
    return 0;
}
x=31, y=502, z=502
x=31, y=500, z=500
x=31, y=498, z=498
x=31, y=504, z=504
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
92 comments Page 1 of 10.

Sahibpreet Singh said:   2 years ago
*(value of address) and increment/decrement operators lie in the same level of precedence. Their precedence level proceeds in right to left manner.

So, if ++ is to the left of * then * will be evaluated first.

Similarly, (in this case) ++ is to the right of * so y++ is evaluated first. *y++ is same as *(y++) and ++*y is same as ++(*y).
(8)

Chidananda said:   5 years ago
Since they mentioned integer is 4 bytes. If we increment from 500 address it will take next 4th address i.e is 504.
(7)

Priya kulshreshtha said:   8 years ago
& deals with the address of the variable mark that point.
So here y=&x
&x means address of x
What is the address of x?
It is 500
So y=500
Point it that the value stored in y is the address of another variable y.

Again it says
Z=y
Z=500
Now,

*y++=*z++
Let's break the term
* in front of the pointer returns the value
Meaning *y point to some other variable
That is
Y=&x
So *y=30
And it needs to be incremented because of
*y++

But if we consider the right side of y assignment
There is *z++
That stores the address 500
And since it has to be incremented
And here address increment must be from 4
That is 504
So here y=z= incremented value that is 504.
(6)

Shubham said:   6 years ago
#include<stdio.h>

int main()
{
int x=30, *y, *z;
y=&x; /* Assume address of x is 500 and integer is 4 byte size */
z=y; // initialization z by 500
*y++=*z++; // pointer size is 4 byte when we use the increment operater at that time pointer incremented by the pointer size.
x++;
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
(4)

Nitis said:   6 years ago
Address size will be 2 byte, then how it can be 504?

It should 502.
(2)

Tpr said:   8 years ago
*y++=*z++;.

Is above statement means 504=504?
(2)

Ramanathan said:   1 year ago
@All.

Here y and z point to the same location and that contains the address of var x and dereferences to value 30.

Then post increment ++ has higher precedence than * this the address of y gets incremented but still. Not assigned to ptr var y as it's post decrement. The updated address is shown only while printing it at the final statement so the address remains the same now similar concept applies to *z++ thus no changes happen inside this statement next x gets incremented this at the final statement we get the incremented value of x i e 31 and the updated address of y and z i.e the postfix operation has come into light only after its use after post increment line.

i.e
Say int x=30 now if I printf("%d",x++);
I still get 30 as it isn't assigned (incremented value ) to var x. It is only after this block finishes execution.
Now again printf("%d",x) and you get the incremented value i.e 31.
(1)

Tejaswi Gunti said:   10 years ago
In case of *y++ and *z++ the precedence is from right to left. So first the address of y and z is incremented and then the value is assigned because of the indirection (*).

int size is 4 bytes so address becomes address +4. So the answer is D.

i.e, x=31 (since x++).

y = 504 (If we take the base address as 500).

z = 504 (If we take base address as 500).
(1)

Jacky said:   1 decade ago
Thank all
Finally I understood!
Follow condition of this exercise
y = &x; It mean y point to x that allocate at address 500
z = y; mean z point to address 500 too.
*y++ = *z++; => *(y++) = *(z++) It means increase the address of y and z by 4. So when we printf them, we have result are y = z = 504
(1)

Ramesh chand rebari said:   8 years ago
Here the value of x =30;
We will consider that the address of x is 500;
And very integer have size 4 byte, so that *y++=*z++ are equal having both address value 500 and incremented with one time and value of x is also incremented by one that's why x having 31 and y=z having 504.
(1)


Post your comments here:

Your comments will be displayed after verification.