Sams Teach Yourself C++ in 21 Days

(singke) #1
Understanding Pointers 253

8



  1. What is the difference between the indirection operator and the address-of
    operator?

  2. What is the difference between const int ptrOneand int const ptrTwo?


Exercises ........................................................................................................



  1. What do these declarations do?
    a. int pOne;
    b. int vTwo;
    c. int
    pThree = &vTwo;

  2. If you have an unsigned shortvariable named yourAge, how would you declare a
    pointer to manipulate yourAge?

  3. Assign the value 50 to the variable yourAgeby using the pointer that you declared
    in Exercise 2.

  4. Write a small program that declares an integer and a pointer to integer. Assign the
    address of the integer to the pointer. Use the pointer to set a value in the integer
    variable.

  5. BUG BUSTERS:What is wrong with this code?
    #include
    using namespace std;
    int main()
    {
    int pInt;
    pInt = 9;
    cout << “The value at pInt: “ << pInt;
    return 0;
    }
    6.BUG BUSTERS:What is wrong with this code?
    #include
    using namespace std;
    int main()
    {
    int SomeVariable = 5;
    cout << “SomeVariable: “ << SomeVariable << endl;
    int
    pVar = & SomeVariable;
    pVar = 9;
    cout << “SomeVariable: “ << *pVar << endl;
    return 0;
    }

Free download pdf