if the following code happened int x6 int y8 int yptry yptrx
if the following code happened:
int x=6;
int y=8;
int *yptr=&y;
yptr=&x;
what would *yptr equal
what does this code do
Solution
*yptr will contains the value reside in address of x, ie, the value of x, ie 6;
Please find below the line by line explanation of what is being doing :
int x=6; //initialize variable x with value 6
int y=8; //initialize variable y with value 8
int *yptr=&y; //pointer yptr points to the address location of y, thus here at this stage we will get the value of *yptr as value of y, which is = 8
yptr=&x; //here in this step the yptr changed its pointing location to the address of x (&x). Thus at this stage the *yptr will have the value which resides in the address location of x, which is equal to 6.
Thus *yptr will outputs 6 at the end of this code fragment.
