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 4 of 10.
Kiran Kumar Y said:
1 decade ago
y=&x; /* Assume address of x is 500 and integer is 4 byte size */
z=y;
*y++=*z++;
x++;
here remember 3 points..
initially v knw d value of x = 30, which is incremented, then x=31.
the address of( *z) z is 500 bcoz z=y and x=&y , when incrementing this the value of integer added to it then 504. similarly yalso 504..
z=y;
*y++=*z++;
x++;
here remember 3 points..
initially v knw d value of x = 30, which is incremented, then x=31.
the address of( *z) z is 500 bcoz z=y and x=&y , when incrementing this the value of integer added to it then 504. similarly yalso 504..
Rajesh Gowda said:
1 decade ago
I'm gonna explain *y++=*z++
here ++ is the post increment . when u use ++ operator with assignment operator(=), first assignment takes place then the value will be incremented by one. see here y++=z++ is the two step process 1) y=z
2) increment the value of z by one and increment the value of y by one
here ++ is the post increment . when u use ++ operator with assignment operator(=), first assignment takes place then the value will be incremented by one. see here y++=z++ is the two step process 1) y=z
2) increment the value of z by one and increment the value of y by one
Xstream said:
1 decade ago
In all the operating system pointers will be incremented by 4 bytes. so the answer is D(504)
Nibash pandey said:
1 decade ago
y=&x so y=500;
z=y so y=z=500;
so *y++=*z++=504
and x++=31
so ans is: x=31, y=504, z=504
z=y so y=z=500;
so *y++=*z++=504
and x++=31
so ans is: x=31, y=504, z=504
Neeraj said:
1 decade ago
just try the following program and u will get the difference between *ptr++ and ++*ptr
# include<stdio.h>
int main()
{
int arr[]={15,6,9,22,8,21};
int i,*ptr;
ptr=arr;
for(i=0;i<6;i++)
printf("%d\t",*ptr++);
return 0;
}
Now replace printf() statement with:
printf("%d\t",++*ptr);
In *ptr++,the pointer is incrememted whereas in ++*ptr,the vaue is incremented.
# include<stdio.h>
int main()
{
int arr[]={15,6,9,22,8,21};
int i,*ptr;
ptr=arr;
for(i=0;i<6;i++)
printf("%d\t",*ptr++);
return 0;
}
Now replace printf() statement with:
printf("%d\t",++*ptr);
In *ptr++,the pointer is incrememted whereas in ++*ptr,the vaue is incremented.
Devanshmody said:
1 decade ago
500 is memory address and space occupied is 4bytes.
Ashish kumar said:
1 decade ago
If both *p++ & ++*p used together in one program than *p++ increment the value & ++*p increment the address.
If it is used in the program alone than it is only increase the value.*p++ & (*p)++ are not same.*p++ increase the address array value else (*p)++ increase the array value.
If it is used in the program alone than it is only increase the value.*p++ & (*p)++ are not same.*p++ increase the address array value else (*p)++ increase the array value.
Amit_Nawale said:
1 decade ago
*y++=*z++ is explicitly written as
*y = *z;
y++;
z++;
*y = *z;
y++;
z++;
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!
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
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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers