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 9 of 10.

LOIS said:   9 years ago
It's just because the address of x is not 500. You can start by display it after its declaration.

printf ("x=%d \n", &x);

The output will be 2293100. Now the incremented value will be as you have: 2293104.

Naveena said:   9 years ago
I understood the answer but I have some confusion why not the answer is 31,31,31 because if we remove the line *y++=*z++; it is printing *y=30,*z=30, then if we increase *y++ it has to be 31 as *y is 30 and also for *z but why it incrementing address, why not value but while printing *y why it is not printing 500 but it is 30?

Pease give me the clear explanation.

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)

Prince said:   8 years ago
Can anyone tell depending on the size of integer *y++ value will increment by 4?

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

Is above statement means 504=504?
(2)

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)

PRAVEER said:   6 years ago
It is already given that size is 4.

As it depends on 16byte or 32byte?
(1)

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)


Post your comments here:

Your comments will be displayed after verification.