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

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.

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.

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
(1)

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!

Amit_Nawale said:   1 decade ago
*y++=*z++ is explicitly written as

*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.

Devanshmody said:   1 decade ago
500 is memory address and space occupied is 4bytes.

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.

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

Xstream said:   1 decade ago
In all the operating system pointers will be incremented by 4 bytes. so the answer is D(504)


Post your comments here:

Your comments will be displayed after verification.