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

a) int *number;
cout << number << endl;
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;

a) int *number;
cout << number << endl;
ANS: Pointer number does not "point" to a valid address. Assigning an address to number
would correct the problem.
b) double *realPtr;
long *integerPtr;
integerPtr = realPtr;
ANS: A pointer of type double cannot be directly assigned to a pointer of type long.
c) int * x, y;
x = y;

ANS: Variable y is not a pointer, and therefore cannot be assigned to x. Change the assign-
ment statement to x = &y;.

d) char s[] = "this is a character array";
for ( ; *s != '\0'; s++ )
cout << *s << ' ';
ANS: s is not a modifiable value. Attempting to use operator ++ is a syntax error. Changing
to [] notation corrects the problem as in:
for ( int t = 0; s[ t ] != '\0'; t++ )
cout << s[ t ] << ' ';
e) short *numPtr, result;
void *genericPtr = numPtr;
result = *genericPtr + 7;
ANS: A void * pointer cannot be dereferenced.

Computer Science & Information Technology

You might also like to view...

What is the keyboard shortcut for flash fill?

A) Ctrl + E B) Ctrl + F C) Ctrl + F2 D) Alt + E

Computer Science & Information Technology

A style sheet file has the file extension .css which distinguishes it from an HTML file.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology