<<< Properties of a C++ variable     Index     Storing address of a variable >>>

2. Address of a C++ variable

  • The ampersand operator & returns address of the variable:

    
    #include <iostream>
    using namespace std;
    int main( )
    {
        int x = 5;
        double d = 0.1;
    
        // Taking variable addresses:
        cout << &x << '\n';
        cout << &d << '\n';
    
        return 0;
    }
    
    /* Program might print something like this:
    0012FF2C
    0012FF1C
    */
    
    
  • expression &x returns address of x

  • expression &d returns address of d

  • In C++, taking address of a variable is so common, that a separate punctuation syntax was dedicated to this operation!

<<< Properties of a C++ variable     Index     Storing address of a variable >>>