8.1 Answer each of the following:
A pointer is a variable that contains as its value the of another variable.
A pointer should be initialized to or .
The only integer that can be assigned directly to a pointer is .
8.2 State whether each of the following is true or false. If the answer is false, explain why.
The address operator &
can be applied only to constants and to expressions.
A pointer that is declared to be of type void*
can be dereferenced.
A pointer of one type can’t be assigned to one of another type without a cast operation.
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.
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
.
Declare a pointer nPtr
that points to a variable of type double
.
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.
Write two separate statements that each assign the starting address of built-in array numbers
to the pointer variable nPtr
.
Use a for
statement to display the elements of built-in array numbers
using pointer/offset notation with pointer nPtr
.
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.
Use a for
statement to display the elements of built-in array numbers
using pointer/subscript notation with pointer nPtr
.
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
.
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?
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?
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
.
Declare the variable doublePtr
to be a pointer to an object of type double
and initialize the pointer to nullptr
.
Assign the address of variable number1
to pointer variable doublePtr
.
Display the value of the object pointed to by doublePtr
.
Assign the value of the object pointed to by doublePtr
to variable number2
.
Display the value of number2
.
Display the address of number1
.
Display the address stored in doublePtr
. Is the address the same as that of number1
?
8.5 Perform the task specified by each of the following statements:
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.
Write the function prototype without parameter names for the function in part (a).
Write two statements that each initialize the built-in array of char
s named vowel
with the string of vowels, "AEIOU"
.
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};
++zPtr;
// use pointer to get first value of a built-in array
number = zPtr;
// assign built-in array element 2 (the value 3) to number
number = *zPtr[2];
// display entire built-in array z
for (size_t i{0}; i <= 5; ++i) {
cout << zPtr[i] << endl;
}
++z;