<<< Storing address of a variable     Index     Pointer "vocalization" >>>

4. Pointer to a C++ variable

  • Solution:

    
    int main( )
    {
        int x = 5;
        double d = 0.1;
    
        // Variable sizes:
        int x_bytes = sizeof( x );
        int d_bytes = sizeof( d );
    
        // We need pointers to store addresses:
        int* x_ptr = &x;
        double* d_ptr = &d;
    
        return 0;
    }
    
    
  • Simply taking an address of a variable would be meaningless!

  • In addition to address, we must recognize the object type.

  • Therefore, we need a special variable to accommodate both type and address.

  • Declaration statement

    
        int* x_ptr = &x;
    
    

    instantiates a pointer variable named x_ptr, initialized by the address of variable x.

<<< Storing address of a variable     Index     Pointer "vocalization" >>>