Self-Review Exercises

  1. 8.1 Answer each of the following:

    1. A pointer is a variable that contains as its value the            of another variable.

    2. A pointer should be initialized to            or           .

    3. The only integer that can be assigned directly to a pointer is           .

  2. 8.2 State whether each of the following is true or false. If the answer is false, explain why.

    1. The address operator & can be applied only to constants and to expressions.

    2. A pointer that is declared to be of type void* can be dereferenced.

    3. A pointer of one type can’t be assigned to one of another type without a cast operation.

  3. 8.3 For each of the following, write C++ statements that perform the specified task. Assume that double-precision, floating-point numbers are stored in eight bytes and that the starting address of the built-in array is at location 1002500 in memory. Each part of the exercise should use the results of previous parts where appropriate.

    1. Declare a built-in array of type double called numbers with 10 elements, and initialize the elements to the values 0.0, 1.1, 2.2, …, 9.9. Assume that the constant size has been defined as 10.

    2. Declare a pointer nPtr that points to a variable of type double.

    3. Use a for statement to display the elements of built-in array numbers using array subscript notation. Display each number with one digit to the right of the decimal point.

    4. Write two separate statements that each assign the starting address of built-in array numbers to the pointer variable nPtr.

    5. Use a for statement to display the elements of built-in array numbers using pointer/offset notation with pointer nPtr.

    6. Use a for statement to display the elements of built-in array numbers using pointer/offset notation with the built-in array’s name as the pointer.

    7. Use a for statement to display the elements of built-in array numbers using pointer/subscript notation with pointer nPtr.

    8. Refer to the fourth element of built-in array numbers using array subscript notation, pointer/offset notation with the built-in array’s name as the pointer, pointer subscript notation with nPtr and pointer/offset notation with nPtr.

    9. Assuming that nPtr points to the beginning of built-in array numbers, what address is referenced by nPtr + 8? What value is stored at that location?

    10. Assuming that nPtr points to numbers[5], what address is referenced by nPtr after nPtr -= 4 is executed? What’s the value stored at that location?

  4. 8.4 For each of the following, write a statement that performs the specified task. Assume that double variables number1 and number2 have been declared and that number1 has been initialized to 7.3.

    1. Declare the variable doublePtr to be a pointer to an object of type double and initialize the pointer to nullptr.

    2. Assign the address of variable number1 to pointer variable doublePtr.

    3. Display the value of the object pointed to by doublePtr.

    4. Assign the value of the object pointed to by doublePtr to variable number2.

    5. Display the value of number2.

    6. Display the address of number1.

    7. Display the address stored in doublePtr. Is the address the same as that of number1?

  5. 8.5 Perform the task specified by each of the following statements:

    1. Write the function header for a function called exchange that takes two pointers to double-precision, floating-point numbers x and y as parameters and does not return a value.

    2. Write the function prototype without parameter names for the function in part (a).

    3. Write two statements that each initialize the built-in array of chars named vowel with the string of vowels, "AEIOU".

  6. 8.6 Find the error in each of the following program segments. Assume the following declarations and statements:

    
    int* zPtr; // zPtr will reference built-in array z
    int number;
    int z[5]{1, 2, 3, 4, 5};
    
    1. ++zPtr;

    2.  

      
      // use pointer to get first value of a built-in array
      number = zPtr;
      
    3.  

      
      // assign built-in array element 2 (the value 3) to number
      number = *zPtr[2];
      
    4.  

      
      // display entire built-in array z
      for (size_t i{0}; i <= 5; ++i) {
         cout << zPtr[i] << endl;
      }
      
    5. ++z;

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset