Answers to Self-Test Exercises

  1. 1. A pointer is the memory address of a variable.

  2. 2. To the unwary, or to the neophyte, this looks like two objects of type pointer to int, that is, int*. Unfortunately, the * binds to the identifier, not to the type (that is, not to the int). The result is that this declaration declares intPtr1 to be an int pointer, while intPtr2 is just an ordinary int variable.

  3. 3.

    int *p;          //This declares a pointer variable that can
                        //hold a pointer to an int variable.
    *p = 17;            //Here, * is the dereference operator.
    //This assigns 17 to the memory location pointed to by p.
  4. 4.

    10 20
    20 20
    30 30

    If you replace *p1 = 30; with *p2 = 30;, the output would be the same.

  5. 5.

    10 20
    20 20
    30 20
  6. 6. delete p;

  7. 7.

    typedef int* NumberPtr;
    NumberPtr myPoint;
  8. 8. The new operator takes a type for its argument. new allocates space on the freestore of an appropriate size for a variable of the type of the argument. It returns a pointer to that memory (that is, a pointer to that new dynamic variable), provided there is enough available memory in the freestore. If there is not enough memory available in the freestore, your program ends.

  9. 9. typedef char* CharArray;

  10. 10.

    cout   << "Enter 10 integers:\n";
    for (int i = 0; i < 10; i++)
            cin >> entry[i];
  11. 11. delete [] entry;

  12. 12. 0 1 2 3 4 5 6 7 8 9

  13. 13. 10 1 2 3 4 5 6 7 8 9

  14. 14. 0 1 2 3 4 5 6 7 8 9

  15. 15. 1 2 3 4 5 6 7 8 9