/*
    References and pointers.  (C++ only)

    Nyhoff p. 359 / Chapter 15.
*/

#include <iostream>
using namespace std;

int main()
{
    double  x=42.0;
    double& y=x;    // References.   Remember x,y two names for the same place in memory
    double z=128.0;
    
    cout << "x=" << x << "   y=" << y << "    z=" << z << endl;

    y=12.0;
    cout << "x=" << x << "   y=" << y << "    z=" << z << endl;

    // A reference can't be detached from what it refers to.
    y=z;                      // y is still x, hence y=z means x=y  gets the value of z.

    cout << "x=" << x << "   y=" << y << "    z=" << z << endl;
   
    
    // Pointers to memory addresses  p is now a pointer to memory.  *p is its value.
    // Pointers can be reassigned!

    double* p=&x;

    cout << "x=" << x << "   y=" << y << "    z=" << z << endl;
    cout << "p=" << p << "    p points to " << *p << endl;

    p=&z;                    // Pointers are promiscous! Changed where p points in memory
    cout << "p=" << p << "    p points to " << *p << endl << endl;

    *p=44.0;                  // changed the value that p points to.
    cout << "x=" << x << "   y=" << y << "    z=" << z << endl;

    return 0;
}

