www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 459 (continued)]

Answers to Self-Review Exercises

8.1

a) address. b) 0, NULL, an address. c)0.

8.2
  1. False. The operand of the address operator must be an lvalue; the address operator cannot be applied to constants or to expressions that do not result in references.

  2. False. A pointer to void cannot be dereferenced. Such a pointer does not have a type that enables the compiler to determine the number of bytes of memory to dereference and the type of the data to which the pointer points.

  3. False. Pointers of any type can be assigned to void pointers. Pointers of type void can be assigned to pointers of other types only with an explicit type cast.

8.3
  1. double numbers[ SIZE ] = { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9 };

  2. double *nPtr;

  3. cout << fixed << showpoint << setprecision( 1 );
    for ( int i = 0; i < SIZE; i++ )
       cout << numbers[ i ] <<' ';
    


  4. [Page 460]
  5. nPtr = numbers;
    nPtr = &numbers[ 0 ];
    

  6. cout << fixed << showpoint << setprecision( 1 );
    for ( int j = 0; j < SIZE; j++ )
       cout << *( nPtr + j ) << ' ';
    

  7. cout << fixed << showpoint << setprecision( 1 );
    for ( int k = 0; k < SIZE; k++ )
       cout << *( numbers + k ) << ' ';
    

  8. cout << fixed << showpoint << setprecision( 1 );
    for ( int m = 0; m < SIZE; m++ )
       cout << nPtr[ m ] << ' ';
    

  9. numbers[ 3 ]
    *( numbers + 3 )
    nPtr[ 3 ]
    *( nPtr + 3 )
    

  10. The address is 1002500 + 8 * 8 = 1002564. The value is 8.8.

  11. The address of numbers[ 5 ] is 1002500 + 5 * 8 = 1002540.

    The address of nPtr -= 4 is 1002540 - 4 * 8 = 1002508.

    The value at that location is 1.1.

8.4
  1. double *fPtr;

  2. fPtr = &number1;

  3. cout << "The value of *fPtr is " << *fPtr << endl;

  4. number2 = *fPtr;

  5. cout << "The value of number2 is " << number2 << endl;

  6. cout << "The address of number1 is " << &number1 << endl;

  7. cout << "The address stored in fPtr is " << fPtr << endl;

    Yes, the value is the same.

  8. strcpy( s1, s2 );

  9. cout << "strcmp(s1, s2) = " << strcmp( s1, s2 ) << endl;

  10. strncat( s1, s2, 10 );

  11. cout << "strlen(s1) = " << strlen( s1 ) << endl;

  12. ptr = strtok( s2, "," );

8.5
  1. void exchange( double *x, double *y )

  2. void exchange( double *, double * );

  3. int evaluate( int x, int (*poly)( int ) )

  4. int evaluate( int, int (*)( int ) );

  5. char vowel[] = "AEIOU";

    char vowel[] = { 'A', 'E', 'I', 'O', 'U', '\0' };

8.6
  1. Error: zPtr has not been initialized.

    Correction: Initialize zPtr with zPtr = z;

  2. Error: The pointer is not dereferenced.

    Correction: Change the statement to number = *zPtr;

  3. Error: zPtr[ 2 ] is not a pointer and should not be dereferenced.

    Correction: Change *zPtr[ 2 ] to zPtr[ 2 ].

  4. Error: Referring to an array element outside the array bounds with pointer subscripting.

    Correction: To prevent this, change the relational operator in the for statement to <.

  5. Error: Dereferencing a void pointer.

    Correction: To dereference the void pointer, it must first be cast to an integer pointer. Change the statement to number = *static_cast< int * >( sPtr );


  6. [Page 461]
  7. Error: Trying to modify an array name with pointer arithmetic.

    Correction: Use a pointer variable instead of the array name to accomplish pointer arithmetic, or subscript the array name to refer to a specific element.

  8. Error: Function strncpy does not write a terminating null character to array s, because its third argument is equal to the length of the string "hello".

    Correction: Make 6 the third argument of strncpy or assign '\0' to s[ 5 ] to ensure that the terminating null character is added to the string.

  9. Error: Character array s is not large enough to store the terminating null character.

    Correction: Declare the array with more elements.

  10. Error: Function strcmp will return 0 if the strings are equal; therefore, the condition in the if statement will be false, and the output statement will not be executed.

    Correction: Explicitly compare the result of strcmp with 0 in the condition of the if statement.

8.7
  1. jill

  2. jack and jill

  3. 8

  4. 13


Previous Page
Next Page