### can you tell me whats the output here

int x=9;
int *yptr;
yptr=&x;
yptr+=2;

cout<<*yptr;
system("pause");
}
The code has undefined behavior. Nobody can say what will be outputed.:)
Last edited on
It would be whatever value that is stored in the x's address + 2
you have int x = 9
and the pointer *yptr
&x is what obtains the address from x
yptr is obtaining this address as the value

so the in a quick example
yptr is = x's address + 2
so *yptr is now taking the value of this new address and will output it

yptr=100 + 2
*yptr=value(102)
Last edited on
 @gobiking It would be whatever value that is stored in the x's address + 2

More precisely it would be said
 in the x's address + 2 * sizeof( int )
Then it would be 11 is that right ??
yeah i changed it right after i realized the yptr+=2 was not *yptr+=2 XD
As I said nobody can say what will be outputed. The code has undefined behavior. We do not know whether there is an object of type int at address &x + 2
is this a homework assignment for you because you can just type in an ide and it will tell you what you will get.
Last edited on
yaraa wrote:
Then it would be 11 is that right ??
No, it would be random memory.

Note that there is a very huge different between these two lines of code:
 ``12`` ``````yptr += 2; //change value in yptr *yptr += 2; //change value at yptr ``````
Last edited on
Topic archived. No new replies allowed.