[Page 459 (
continued
)]
Answers to Self-Review Exercises
|
8.1
|
a) address. b)
,
NULL
, an address. c)
.
|
|
8.2
|
-
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.
-
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.
-
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
|
-
double
numbers
[
SIZE
] = {
0.0
,
1.1
,
2.2
,
3.3
,
4.4
,
5.5
,
6.6
,
7.7
,
8.8
,
9.9
};
-
double
*nPtr;
-
cout << fixed << showpoint << setprecision(
1
);
for
(
int
i =
; i <
SIZE
; i++ )
cout << numbers[ i ] <<
' '
;
[Page 460]
-
nPtr = numbers;
nPtr = &numbers[
];
-
cout << fixed << showpoint << setprecision(
1
);
for
(
int
j =
; j <
SIZE
; j++ )
cout << *( nPtr + j ) <<
' '
;
-
cout << fixed << showpoint << setprecision(
1
);
for
(
int
k =
; k <
SIZE
; k++ )
cout << *( numbers + k ) <<
' '
;
-
cout << fixed << showpoint << setprecision(
1
);
for
(
int
m =
; m <
SIZE
; m++ )
cout << nPtr[ m ] <<
' '
;
-
numbers[
3
]
*( numbers +
3
)
nPtr[
3
]
*( nPtr +
3
)
-
The address is
1002500 + 8 * 8 = 1002564
. The value is
8.8
.
-
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
|
-
double
*fPtr;
-
fPtr = &number1;
-
cout <<
"The value of *fPtr is "
<< *fPtr << endl;
-
number2 = *fPtr;
-
cout <<
"The value of number2 is "
<< number2 << endl;
-
cout <<
"The address of number1 is "
<< &number1 << endl;
-
cout <<
"The address stored in fPtr is "
<< fPtr << endl;
Yes, the value is the same.
-
strcpy
( s1, s2 );
-
cout <<
"strcmp(s1, s2) = "
<< strcmp( s1, s2 ) << endl;
-
strncat
( s1, s2,
10
);
-
cout <<
"strlen(s1) = "
<< strlen( s1 ) << endl;
-
ptr =
strtok
( s2,
","
);
|
|
8.5
|
-
void
exchange(
double
*x
,
double
*y )
-
void
exchange(
double
*
,
double
* )
;
-
int
evaluate(
int
x,
int
(*poly)(
int
) )
-
int
evaluate(
int
,
int
(*)(
int
) );
-
char
vowel[] =
"AEIOU"
;
char
vowel[] = {
'A', 'E', 'I', 'O', 'U', ''
};
|
|
8.6
|
-
Error:
zPtr
has not been
initialized
.
Correction: Initialize
zPtr
with
zPtr = z
;
-
Error: The pointer is not dereferenced.
Correction: Change the statement to
number = *zPtr;
-
Error:
zPtr[ 2 ]
is not a pointer and should not be dereferenced.
Correction: Change
*zPtr[ 2 ]
to
zPtr[ 2 ]
.
-
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
<
.
-
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 );
[Page 461]
-
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.
-
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
''
to
s[ 5 ]
to ensure that the terminating null character is added to the string.
-
Error: Character array
s
is not large enough to store the terminating null character.
Correction: Declare the array with more elements.
-
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
in the condition of the
if
statement.
|
|
8.7
|
-
jill
-
jack and jill
-
8
-
13
|
|