www.gibmonks.com

Main Page

[Page 457 (continued)]

### Self-Review Exercises

 8.1 Answer each of the following: A pointer is a variable that contains as its value the____________ of another variable.The three values that can be used to initialize a pointer are_____________,__________ and___________.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. The address operator & can be applied only to constants and to expressions.A pointer that is declared to be of type void * can be dereferenced.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]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.Declare a pointer nPtr that points to a variable of type double.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.Write two separate statements that each assign the starting address of array numbers to the pointer variable nPtr.Use a for statement to print the elements of array numbers using pointer/offset notation with pointer nPtr.Use a for statement to print the elements of array numbers using pointer/offset notation with the array name as the pointer.Use a for statement to print the elements of array numbers using pointer/subscript notation with pointer nPtr.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.Assuming that nPtr points to the beginning of array numbers, what address is referenced by nPtr + 8? What value is stored at that location?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. Declare the variable fPtr to be a pointer to an object of type double.Assign the address of variable number1 to pointer variable fPtr.Print the value of the object pointed to by fPtr.Assign the value of the object pointed to by fPtr to variable number2.Print the value of number2.Print the address of number1.Print the address stored in fPtr. Is the value printed the same as the address of number1?Copy the string stored in array s2 into array s1.Compare the string in s1 with the string in s2, and print the result.Append the first 10 characters from the string in s2 to the string in s1.Determine the length of the string in s1, and print the result.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: 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.Write the function prototype for the function in part (a).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.Write the function prototype for the function in part (c).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 }; ``` ++zPtr; ```// use pointer to get first value of array number = zPtr; ``` ```// assign array element 2 (the value 3) to number number = *zPtr[ 2 ]; ``` ```// print entire array z for ( int i = 0; i <= 5; i++ ) cout << zPtr[ i ] << endl; ``` ```// assign the value pointed to by sPtr to number number = *sPtr; ``` ++z; ```char s[ 10 ]; cout << strncpy( s, "hello", 5 ) << endl; ``` ```char s[ 12 ]; strcpy( s, "Welcome Home"); ``` ```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 ]; ``` ```cout << strcpy( s3, s2 ) << endl; ``` ```cout << strcat( strcat( strcpy( s3, s1 ), " and " ), s2 ) << endl; ``` ```cout << strlen( s1 ) + strlen( s2 ) << endl; ``` ```cout << strlen( s3 ) << endl; ```