Find the error in each of the following segments. If the error can be corrected, explain how.

```
a) int* number;
b) double* realPtr; long* integerPtr; integerPtr = realPtr;
c) int* x, y;
x = y;
d) char s[]{"this is a character array"};
for (; *s != '\0'; ++s) {
cout << *s << ' ';
}
e) short* numPtr, result; void* genericPtr{numPtr}; result = *genericPtr + 7;
f) double x = 19.34;
double xPtr{&x};
cout << xPtr << endl;
```

a) Pointer number does not "point" to a valid address—assigning a valid address of an int to number would correct this the problem. Also, number is not dereferenced in the output statement.
b) A pointer of type double cannot be directly assigned to a pointer of type long.
c) Variable y is not a pointer, and therefore cannot be assigned to x. Change the assign- ment statement to x = &y; or declare y as a pointer.
d) s is not a modifiable value. Attempting to use operator ++ is a syntax error. Changing to [] notation corrects the problem as in:
e) A void * pointer cannot be dereferenced
f) xPtr is not a pointer and therefore cannot be assigned an address. Change xPtr’s type to double* to correct the problem. The cout statement display’s the address to which xPtr points (once the previous correction is made)—this is not an error, but normally you’d output the value of what the pointer points to, not the address stored in the pointer.

Computer Science & Information Technology

You might also like to view...

Which of the following can be used to make a forensic copy?

a. Access Data FTK b. Guidance Software EnCase c. Linux dd command d. Any of the above

Computer Science & Information Technology

The ____________________ object allows the user to select one date from a calendar of dates and times.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology