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 8 of 10.
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.
Srk said:
1 decade ago
Consider value of x is stored in address 500.
So y = z = 500.
If we increment the value of the pointer it will be incremented to point next location where a new integer will be stored.
*z++=500+sizeof (int) *1 == 504.
Same thing happens for y.
So y = z = 500.
If we increment the value of the pointer it will be incremented to point next location where a new integer will be stored.
*z++=500+sizeof (int) *1 == 504.
Same thing happens for y.
Garima said:
1 decade ago
x++ means increment of 1 in x value. So x=31.
Same as *y++ and *z++ but difference is integer x contain 4 byte size so the value of *y++ and *z++ is 504 because x address is 500.
Same as *y++ and *z++ but difference is integer x contain 4 byte size so the value of *y++ and *z++ is 504 because x address is 500.
Amol said:
1 decade ago
*y++=*z++ can broken down as
*y=*z;
y++;
z++;
Because it is post increment operator.
*y=*z;
y++;
z++;
Because it is post increment operator.
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.
Naga Linga Murthy Doddipatla said:
1 decade ago
Here address of x is 500 and value is 30
//value of int takes 4byte
y=&x // y=500
z=y // z=500
*y++ = *z++ //both will incremented by 4bytes
x++ //value incremented by 1
After execution x=31, y=504, z=504.
//value of int takes 4byte
y=&x // y=500
z=y // z=500
*y++ = *z++ //both will incremented by 4bytes
x++ //value incremented by 1
After execution x=31, y=504, z=504.
Divyaradhakrishnan said:
1 decade ago
In post increment the value will be first assigned and then only it will get incremented and here how the value of X is printed as 31. Since I am non csc I can't understand any one explain in basic.
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!
Amit_Nawale said:
1 decade ago
*y++=*z++ is explicitly written as
*y = *z;
y++;
z++;
*y = *z;
y++;
z++;
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers