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;
}
Discussion:
92 comments Page 1 of 10.
Samiksha said:
1 decade ago
Compile the code in the C Compiler provided and add a printf() before incrementing the values, you'll get the difference:
/* Note: GCC Compiler (32 Bit Linux Platform). */
#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;
printf("x=%d, y=%d, z=%d\n", x, y, z);
*y++=*z++;
x++;
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
Output:
x=30, y=500, z=500
x=31, y=496, z=496
The value of x increases by 1, which is normal variable
But the values of *y and *z are seen to decrease by 4 (bytes)
This happens because the memory addresses are arranges in descending order from top to bottom, so when 500 becomes 496, it actually means that the pointer values has INCREASED!!!
The memory addresses look something like this:
|10|
|09|
|08|
|07|
|06|
|05|
|04|
|03|
|02|
|01|
|00|
That is why the answer option provided is wrong as it shows the changed values of *y and *z to be 504, which is actually DECREASING of the memory address!
/* Note: GCC Compiler (32 Bit Linux Platform). */
#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;
printf("x=%d, y=%d, z=%d\n", x, y, z);
*y++=*z++;
x++;
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
Output:
x=30, y=500, z=500
x=31, y=496, z=496
The value of x increases by 1, which is normal variable
But the values of *y and *z are seen to decrease by 4 (bytes)
This happens because the memory addresses are arranges in descending order from top to bottom, so when 500 becomes 496, it actually means that the pointer values has INCREASED!!!
The memory addresses look something like this:
|10|
|09|
|08|
|07|
|06|
|05|
|04|
|03|
|02|
|01|
|00|
That is why the answer option provided is wrong as it shows the changed values of *y and *z to be 504, which is actually DECREASING of the memory address!
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.
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)
Maggie said:
1 decade ago
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=30;//variable x has 30;
y=&x//y has 500;
z=y;//rigth to left ie y has 500 and its assigned to z so z=500
//y and z both pointing to x
*y++=*z++//here as per post increment rules 1st we should assign and then increment so here *z has 30, assign it to y and it sits in *y so IN THIS LINE both y and z has 30
x++//x increments
And while printing
x =31;
And y which is having 500 will be incremented to 504 because in this line *y++ means y++ is 500 to 504 and then *
In this line the value would have got incremented to 504 na
and so is 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=30;//variable x has 30;
y=&x//y has 500;
z=y;//rigth to left ie y has 500 and its assigned to z so z=500
//y and z both pointing to x
*y++=*z++//here as per post increment rules 1st we should assign and then increment so here *z has 30, assign it to y and it sits in *y so IN THIS LINE both y and z has 30
x++//x increments
And while printing
x =31;
And y which is having 500 will be incremented to 504 because in this line *y++ means y++ is 500 to 504 and then *
In this line the value would have got incremented to 504 na
and so is z.
Natwar said:
1 decade ago
@Samiksha.
You wrote that
" The value of x increases by 1, which is normal variable
But the values of *y and *z are seen to decrease by 4 (bytes)
This happens because the memory addresses are arranges in descending order from top to bottom, so when 500 becomes 496, it actually means that the pointer values has INCREASED! "
But I think you are doing mistake in -ve and +ve addresses.
You may got o/p like this:
x=30, y=-4352852, z=-4352852.
x=31, y=-4352848, z=-4352848.
Please check this,
y=-4352852, z=-4352852.
y=-4352848, z=-4352848.
Value of y is negative.
-4352852 + 4 = -4352848.
If value of y is +ve, then o/p will be like:
y=+4352852, z=+4352852.
y=+4352856, z=+4352856.
You wrote that
" The value of x increases by 1, which is normal variable
But the values of *y and *z are seen to decrease by 4 (bytes)
This happens because the memory addresses are arranges in descending order from top to bottom, so when 500 becomes 496, it actually means that the pointer values has INCREASED! "
But I think you are doing mistake in -ve and +ve addresses.
You may got o/p like this:
x=30, y=-4352852, z=-4352852.
x=31, y=-4352848, z=-4352848.
Please check this,
y=-4352852, z=-4352852.
y=-4352848, z=-4352848.
Value of y is negative.
-4352852 + 4 = -4352848.
If value of y is +ve, then o/p will be like:
y=+4352852, z=+4352852.
y=+4352856, z=+4352856.
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.
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)
Raks said:
1 decade ago
Simple concept I have commented the flow just check out.
int main()
{
int x=30, *y, *z;
y=&x; /* Assume address of x is 500 and integer is 4 byte size */
z=y; //both pointers r pointing to same value
*y++=*z++; /* z is post incremented hence original value adress 500 is assigned then incremented but, here even we r incrementing y hence it changes from 500 to 504(it is integer type). */
x++; //incremented in the next line it is printed
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
int main()
{
int x=30, *y, *z;
y=&x; /* Assume address of x is 500 and integer is 4 byte size */
z=y; //both pointers r pointing to same value
*y++=*z++; /* z is post incremented hence original value adress 500 is assigned then incremented but, here even we r incrementing y hence it changes from 500 to 504(it is integer type). */
x++; //incremented in the next line it is printed
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
Abid Ali said:
1 decade ago
first of all x = 30.
x++ means x= 31.
Since the precedence of *(pointer operator) is lower than ++(post increment), *y++ means *(y++). Post increment shows its effect after execution of the very statement. So y gets incremented to 504 but its effect is ignored while executing the statement.
To better understand this take this example.
int main()
{
int i[] = {3, 5};
int *p = i;
int j = --*p++;
printf("j = %d\n\n", j);
return 0;
}
In this the statement --*p++ can be interpreted as --(*(p++)).
x++ means x= 31.
Since the precedence of *(pointer operator) is lower than ++(post increment), *y++ means *(y++). Post increment shows its effect after execution of the very statement. So y gets incremented to 504 but its effect is ignored while executing the statement.
To better understand this take this example.
int main()
{
int i[] = {3, 5};
int *p = i;
int j = --*p++;
printf("j = %d\n\n", j);
return 0;
}
In this the statement --*p++ can be interpreted as --(*(p++)).
Basant Sukhdev said:
1 decade ago
It's very easy...
Here y=z; means both has same address i.e 500..ok...
now this expression *y++=*z++; means "whatever the the VALUE AT Z++ ,assign it to the address of y++"
note:-here *y++ & *z++ both are post increment so...firstly expression(*y++=*z++;) is performed.
so...value at z is assigned to y(i.e nothing but 30)
and after dat both address is incremented by 4 bytes(according to the question)
so finally..
x++;
becomes 31 and z,y becomes 504,504
Here y=z; means both has same address i.e 500..ok...
now this expression *y++=*z++; means "whatever the the VALUE AT Z++ ,assign it to the address of y++"
note:-here *y++ & *z++ both are post increment so...firstly expression(*y++=*z++;) is performed.
so...value at z is assigned to y(i.e nothing but 30)
and after dat both address is incremented by 4 bytes(according to the question)
so finally..
x++;
becomes 31 and z,y becomes 504,504
Sabbul said:
1 decade ago
Here y=z; means both has same address i.e 500.
Now this expression *y++=*z++; means "whatever the the VALUE AT Z++ ,assign it to the address of y++"
Note:-here *y++ & *z++ both are post increment so...firstly expression(*y++=*z++;) is performed.
So...value at z is assigned to y(i.e nothing but 30).
And after that both address is incremented by 4 bytes(according to the question).
So finally,
x++;
Becomes 31 and z, y becomes 504, 504.
Now this expression *y++=*z++; means "whatever the the VALUE AT Z++ ,assign it to the address of y++"
Note:-here *y++ & *z++ both are post increment so...firstly expression(*y++=*z++;) is performed.
So...value at z is assigned to y(i.e nothing but 30).
And after that both address is incremented by 4 bytes(according to the question).
So finally,
x++;
Becomes 31 and z, y becomes 504, 504.
Sourav patra said:
1 decade ago
y=&x; //address of x i.e 500 is assigned to y.
z=y; // pointer y contains address value of x i.e 500 is assigned to z.
*y++=*z++; //first value with in z pointer assigned to y, then z is incremented to next memory location i.e 504. Now y is incremented to next memory location i.e 504.
x++; // value contained by x i.e 30 incremented to next number 31.
printf("x=%d, y=%d, z=%d\n", x, y, z); // values are printed.
z=y; // pointer y contains address value of x i.e 500 is assigned to z.
*y++=*z++; //first value with in z pointer assigned to y, then z is incremented to next memory location i.e 504. Now y is incremented to next memory location i.e 504.
x++; // value contained by x i.e 30 incremented to next number 31.
printf("x=%d, y=%d, z=%d\n", x, y, z); // values are printed.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers