CIS-60 Home: http://www.c-jump.com/CIS60/CIS60syllabus.htm

C++ Pointers


  1. Properties of a C++ variable
  2. Address of a C++ variable
  3. Storing address of a variable
  4. Pointer to a C++ variable
  5. Pointer "vocalization"
  6. Pointer is a also variable
  7. Why use pointers?
  8. Pointer dereference *ptr
  9. Pointer syntax
  10. Pointer summary
  11. String literal
  12. Pointer to character string
  13. Pointer as function argument
  14. Pointers and arrays
  15. Arrays of pointers
  16. Arrays of pointers example
  17. Command-line arguments
  18. Implementing swap( ) function

1. Properties of a C++ variable

  • Consider:

    
    int main( )
    {
        // Local variables:
        int x = 5;
        double d = 0.1;
    
        // Variable sizes:
        int x_bytes = sizeof( x );
        int d_bytes = sizeof( d );
    
        // What about variable addresses ?
        int x_addr = addressof( x );
        int d_addr = addressof( d );
    
        return 0;
    }
    
    
  • Note: this sample will not yet compile because of the addressof.

  • What do we know about x and d?

    • Types are int and double

    • Operator sizeof returns sizes in bytes

    • How about an 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!

3. Storing address of a variable

  • Could we store an addresses of variables in our program?

    
    int main( )
    {
        int x = 5;
        double d = 0.1;
    
        // Variable sizes:
        int x_bytes = sizeof( x );
        int d_bytes = sizeof( d );
    
        // Take and store variable addresses:
        int x_addr = &x;
        int d_addr = &d;
    
        return 0;
    }
    
    
  • But why the code does not compile?..

    • ...because an address needs a special type of storage!

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.

5. Pointer "vocalization"



6. Pointer is a also variable



7. Why use pointers?



8. Pointer dereference *ptr



9. Pointer syntax


  1. Here, the asterisk * is a type modifier, not a dereference.

  2. Ampersand & operator returns an address of a variable: ptr = &x;

  3. Finally, an asterisk *ptr dereferences the pointer, providing access to the variable in memory:

    Dereference operator *ptr is also known as indirection operator.

     


10. Pointer summary



11. String literal


H E L L O \0

12. Pointer to character string



13. Pointer as function argument



14. Pointers and arrays



15. Arrays of pointers


months[ 0 ] = 00476FEC : I l l e g a l \0
months[ 1 ] = 00476FF4 : J a n \0
months[ 2 ] = 00476FF8 : F e b \0
months[ 3 ] = 00476FFC : M a r \0

16. Arrays of pointers example



17. Command-line arguments

  • A program gains access to its command-line arguments as follows:

    
    #include <iostream>
    using namespace std;
    
    int main( int argc, char* argv[] )
    {
        int idx;
        for ( idx = 1; i < argc; ++idx ) {
            cout << argv[ idx ] << '\n';
        }
        return 0;
    }
    
    
  • Note that the loop begins at index 1, thus skipping the name of the program.

  • argc is the argument counter

  • argv[ ] is the array of pointers to strings

  • If command line was

        C:\>prog Hello World
    

    then argc is set to 3, and argv is organized like this:

    argv[0] p r o g \0
    argv[1] H e l l o \0
    argv[2] W o r l d \0
    argv[3] NULL

     

18. Implementing swap( ) function