www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 457 (continued)]

Self-Review Exercises

8.1

Answer each of the following:

  1. A pointer is a variable that contains as its value the____________ of another variable.

  2. The three values that can be used to initialize a pointer are_____________,__________ and___________.

  3. The only integer that can be assigned directly to a pointer is_____________.

8.2

State whether the following are true or false. If the answer is false, explain why.

  1. The address operator & can be applied only to constants and to expressions.

  2. A pointer that is declared to be of type void * can be dereferenced.

  3. Pointers of different types can never be assigned to one another without a cast operation.

8.3

For each of the following, write C++ statements that perform the specified task. Assume that double-precision, floating-point numbers are stored in eight bytes and that the starting address of the array is at location 1002500 in memory. Each part of the exercise should use the results of previous parts where appropriate.


    [Page 458]
  1. Declare an array of type double called numbers with 10 elements, and initialize the elements to the values 0.0, 1.1, 2.2, ..., 9.9. Assume that the symbolic constant SIZE has been defined as 10.

  2. Declare a pointer nPtr that points to a variable of type double.

  3. Use a for statement to print the elements of array numbers using array subscript notation. Print each number with one position of precision to the right of the decimal point.

  4. Write two separate statements that each assign the starting address of array numbers to the pointer variable nPtr.

  5. Use a for statement to print the elements of array numbers using pointer/offset notation with pointer nPtr.

  6. Use a for statement to print the elements of array numbers using pointer/offset notation with the array name as the pointer.

  7. Use a for statement to print the elements of array numbers using pointer/subscript notation with pointer nPtr.

  8. Refer to the fourth element of array numbers using array subscript notation, pointer/offset notation with the array name as the pointer, pointer subscript notation with nPtr and pointer/offset notation with nPtr.

  9. Assuming that nPtr points to the beginning of array numbers, what address is referenced by nPtr + 8? What value is stored at that location?

  10. Assuming that nPtr points to numbers[ 5 ], what address is referenced by nPtr after nPtr -= 4 is executed? What is the value stored at that location?

8.4

For each of the following, write a single statement that performs the specified task. Assume that floating-point variables number1 and number2 have been declared and that number1 has been initialized to 7.3. Assume that variable ptr is of type char *. Assume that arrays s1 and s2 are each 100-element char arrays that are initialized with string literals.

  1. Declare the variable fPtr to be a pointer to an object of type double.

  2. Assign the address of variable number1 to pointer variable fPtr.

  3. Print the value of the object pointed to by fPtr.

  4. Assign the value of the object pointed to by fPtr to variable number2.

  5. Print the value of number2.

  6. Print the address of number1.

  7. Print the address stored in fPtr. Is the value printed the same as the address of number1?

  8. Copy the string stored in array s2 into array s1.

  9. Compare the string in s1 with the string in s2, and print the result.

  10. Append the first 10 characters from the string in s2 to the string in s1.

  11. Determine the length of the string in s1, and print the result.

  12. Assign to ptr the location of the first token in s2. The tokens delimiters are commas (,).

8.5

Perform the task specified by each of the following statements:

  1. Write the function header for a function called exchange that takes two pointers to double-precision, floating-point numbers x and y as parameters and does not return a value.

  2. Write the function prototype for the function in part (a).

  3. Write the function header for a function called evaluate that returns an integer and that takes as parameters integer x and a pointer to function poly. Function poly takes an integer parameter and returns an integer.

  4. Write the function prototype for the function in part (c).

  5. Write two statements that each initialize character array vowel with the string of vowels, "AEIOU".

8.6

Find the error in each of the following program segments. Assume the following declarations and statements:


[Page 459]
int *zPtr; // zPtr will reference array z int *aPtr = 0; void *sPtr = 0; int number; int z[ 5 ] = { 1, 2, 3, 4, 5 };

  1. ++zPtr;

  2. // use pointer to get first value of array
    number = zPtr;
    

  3. // assign array element 2 (the value 3) to number
    number = *zPtr[ 2 ];
    

  4. // print entire array z
    for ( int i = 0; i <= 5; i++ )
       cout << zPtr[ i ] << endl;
    

  5. // assign the value pointed to by sPtr to number
    number = *sPtr;
    

  6. ++z;

  7. char s[ 10 ];
    cout << strncpy( s, "hello", 5 ) << endl;
    

  8. char s[ 12 ];
    strcpy( s, "Welcome Home");
    

  9. if ( strcmp( string1, string2 ) )
       cout << "The strings are equal" << endl;
    

8.7

What (if anything) prints when each of the following statements is performed? If the statement contains an error, describe the error and indicate how to correct it. Assume the following variable declarations:

char s1[ 50 ] = "jack";
char s2[ 50 ] = "jill";
char s3[ 50 ];

  1. cout << strcpy( s3, s2 ) << endl;
    

  2. cout << strcat( strcat( strcpy( s3, s1 ), " and " ), s2 )
       << endl;
    

  3. cout << strlen( s1 ) + strlen( s2 ) << endl;
    

  4. cout << strlen( s3 ) << endl;
    


Previous Page
Next Page