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...

What are the two ways IEEE 802.11 supports authenticating clients?

What will be an ideal response?

Computer Science & Information Technology

When creating a presentation, a theme and a template are the same thing

Indicate whether the statement is true or false

Computer Science & Information Technology

Which of the following loops displays all numbers from 0 to 40 in increments of 5?

A. Declare Numeric num = 0 While evenNum <= 40    Display num    num = num + 5 End While B. Declare Numeric num = 0 While num = 40    Display num    num = num + 5 End While C. Declare Numeric num = 1 While num < 40    Display num    num = num + 5 End While D. Declare Numeric num = 1 While num = 40    Display num    num = num + 5 End While

Computer Science & Information Technology

____ refers to the complete malfunction of a computer system.

A. Computer burnout B. System failure C. A spike D. An interrupted power supply

Computer Science & Information Technology