Reference variable to a pointer

Viewed 201
int main() {

    int x=10;
    int *p=&x;
    int &y =*p;

    cout<< x << endl << &x <<endl;
    cout<< *p << endl << p <<endl;
    cout<< y << endl << &y <<endl;

    p++;
   *p = 20;

   cout<< x << endl << &x <<endl;
   cout<< *p << endl << p <<endl;
   cout<< y <<endl<< &y <<endl;

   return 0;
}

Above is the code which would best explain my question. Normally, a variable reference (&) takes the address of a variable and starts referring to the same. I have tried doing the same through a pointer. I have defined a variable, pointer p points to x and a reference variable y refers to *p. Would this mean that y refers to the same variable x now? As the next step, I stopped pointing to x through *p, what happens to reference variable y now? What would it hold. In the code above, cout<

Can someone help explain the behavior here.

5 Answers
Related