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

Which type of translation should you use if you need 50 computers in the corporate network to be able to access the Internet using a single public IP address?

A. one-to-one NAT B. port address translation C. one-to-many NAT D. DMZ proxy translation

Computer Science & Information Technology

________ was a new design goal that Microsoft strived to attain in Windows XP.

a) Fast system boot up b) Hard real time guarantees c) Linux emulation d) C2 security certification

Computer Science & Information Technology

If you wish to create a postcard you must choose the template from the ________ options dialog box

Fill in the blank(s) with correct word

Computer Science & Information Technology

In a needs assessment project, a charter often specifies how a project's success will be measured or evaluated.

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

Computer Science & Information Technology