Find the error in each of the following program segments. Assume the following declara- tions and statements:
```
int* zPtr; // zPtr will reference built-in array z int number;
int z[5]{1, 2, 3, 4, 5};
```
a) ++zPtr;
b) // use pointer to get first value of a built-in array
c) // assign built-in array element 2 (the value 3) to number
d) // display entire built-in array z
e) ++z;
a) Error: zPtr has not been initialized.
Correction: Initialize zPtr with zPtr = z; (Parts b–e depend on this correction.)
number = zPtr;
b) Error: The pointer is not dereferenced.
Correction: Change the statement to number = *zPtr;
number = *zPtr[2];
c) Error: zPtr[2] is not a pointer and should not be dereferenced.
Correction: Change *zPtr[2] to zPtr[2].
for (size_t i{0}; i <= 5; ++i) {
cout << zPtr[i] << endl;
}
d) Error: Referring to an out-of-bounds built-in array element with pointer subscript- ing.
Correction: To prevent this, change the relational operator in the for statement to <
or change the 5 to a 4.
e) Error: Trying to modify a built-in array’s name with pointer arithmetic.
Correction: Use a pointer variable instead of the built-in array’s name to accomplish pointer arithmetic, or subscript the built-in array’s name to refer to a specific element.
You might also like to view...
In the VBA statement: MsgBox("Do you really want to quit Access?",4,"Confirm please")=6, what is the purpose of the 4?
A) Displays Yes and No buttons B) Inserts 4 lines before "Confirm please" C) Provides 4 spaces to type an answer D) Bolds all text in the MsgBox
Pseudocode does not typically include __________.
(a) definitions (b) input/output (c) action statements (d) control statements