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 3 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..
Sekar said:
1 decade ago
1. The address of x which is assumed as 500 and size is 4 byte passed to pointer y.
2. The address stored in y is passed to pointer z.
3. Then both y and z get post increment that is 500 to 504. because the size is 4 byte.
4. the value of x again incremented to 30 to 31;
ANS is = x = 31, y = 504, and z = 504.
2. The address stored in y is passed to pointer z.
3. Then both y and z get post increment that is 500 to 504. because the size is 4 byte.
4. the value of x again incremented to 30 to 31;
ANS is = x = 31, y = 504, and z = 504.
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)
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
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.
Kavitha said:
1 decade ago
#include<stdio.h>
3 int main()
4 {
5 int a[5]={2,3,1,8,6};
6 int *p,*q,i;
7 p=a;
8 q=a;
9 for(i=0;i<5;i++)
10 printf(" p=%d q=%d\n",(*p)++,(*q)++);
11 }
Output:
p=3 q=2.
p=5 q=4.
p=7 q=6.
p=9 q=8.
p=11 q=10.
3 int main()
4 {
5 int a[5]={2,3,1,8,6};
6 int *p,*q,i;
7 p=a;
8 q=a;
9 for(i=0;i<5;i++)
10 printf(" p=%d q=%d\n",(*p)++,(*q)++);
11 }
Output:
p=3 q=2.
p=5 q=4.
p=7 q=6.
p=9 q=8.
p=11 q=10.
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.
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)
Kidsid said:
1 decade ago
Hey guys the correct solution is that
here it uses the concept of precedence of operators like
*y=*z
*y++;
*z++;
in case of *z++
the ++ has higher precedence than *
so that it increment z by 4 than get that value instead u should try
(*y)++;
check it out??
here it uses the concept of precedence of operators like
*y=*z
*y++;
*z++;
in case of *z++
the ++ has higher precedence than *
so that it increment z by 4 than get that value instead u should try
(*y)++;
check it out??
Vasuvandan said:
1 decade ago
Clarify my particular doubts only?
1) Why compiler says error when I execute y++=z++?
2) Some people said *y++ = *z++ can be done as:
I) *y =*z.
II) y++, z++.
Actually what does mean *y=*z when y=z=500?
3) And also explain right to, left associations?
1) Why compiler says error when I execute y++=z++?
2) Some people said *y++ = *z++ can be done as:
I) *y =*z.
II) y++, z++.
Actually what does mean *y=*z when y=z=500?
3) And also explain right to, left associations?
Rahul reddy said:
1 decade ago
y* and z* are integer data types.so when ++y* and ++*z represents,
++*y=y+4bytes and ++*z=z+4 because * is address of corresponding variable but not its value as integer normally takes 2 bytes but according to comments it is represented as 4 bytes.
++*y=y+4bytes and ++*z=z+4 because * is address of corresponding variable but not its value as integer normally takes 2 bytes but according to comments it is represented as 4 bytes.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers