Address
Question: The three values that can be used to initialize a pointer
are_____________,__________ and___________.
NULL, an address
Question: The only integer that can be assigned directly to a pointer is_____________.
Question: The address operator & can be applied only to constants and to expressions
False
Question: A pointer that is declared to be of type void * can be dereferenced
False
Question: Pointers of different types can never be assigned to one another without a cast
operation
false
Question: 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
double numbers[ SIZE ] = { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9 };
Question: Declare a pointer nPtr that points to a variable of type double
double *nPtr;
Question: 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:
cout << fixed << showpoint << setprecision( 1 );
for ( int i = 0; i < SIZE; i++ )
cout << numbers[ i ] <<' ';
Question: Write two separate statements that each assign the starting address of array
numbers to the pointer variable nPtr.
nPtr = numbers;
nPtr = &numbers[ 0 ];
Question: Use a for statement to print the elements of array numbers using pointer/offset
notation with pointer nPtr
cout << fixed << showpoint << setprecision( 1 );
for ( int j = 0; j < SIZE; j++ )
cout << *( nPtr + j ) << ' ';
Question: Use a for statement to print the elements of array numbers using pointer/offset
notation with the array name as the pointer
cout << fixed << showpoint << setprecision( 1 );
for ( int k = 0; k < SIZE; k++ )
cout << *( numbers + k ) << ' ';
Question: Use a for statement to print the elements of array numbers using
pointer/subscript notation with pointer nPtr
cout << fixed << showpoint << setprecision( 1 );
for ( int m = 0; m < SIZE; m++ )
cout << nPtr[ m ] << ' ';
Question: 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
numbers[ 3 ]
*( numbers + 3 )
nPtr[ 3 ]
*( nPtr + 3 )
Question: Assuming that nPtr points to the beginning of array numbers (the starting
address of the array is at location 1002500 in memory), what address is referenced by
nPtr + 8?
The address is 1002500 + 8 * 8 = 1002564
Question: 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.
double *fPtr;
Question: 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. Assign the
address of variable number1 to pointer variable fPtr.
fPtr = &number1;
Question: 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. Print the
value of the object pointed to by fPtr.
cout << "The value of *fPtr is " << *fPtr << endl;
Question: 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. Assign the
value of the object pointed to by fPtr to variable number2.
number2 = *fPtr;
Question: 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. Print the
value of number2.
cout << "The value of number2 is " << number2 << endl;
Question: 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. Print the
address of number1.
cout << "The address of number1 is " << &number1 << endl;
Question: 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. Print the
address stored in fPtr.
cout << "The address stored in fPtr is " << fPtr << endl;
Question: 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. Copy the
string stored in array s2 into array s1.
strcpy( s1, s2 );
Question: 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. Compare
the string in s1 with the string in s2, and print the result.
cout << "strcmp(s1, s2) = " << strcmp( s1, s2 ) << endl;
Question: Write a single statement that performs the specified task. Assume that
return false">ссылка скрыта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. Append
the first 10 characters from the string in s2 to the string in s1.
strncat( s1, s2, 10 );
Question: 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. Determine
the length of the string in s1, and print the result.
cout << "strlen(s1) = " << strlen( s1 ) << endl;
Question: 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. Assign to
ptr the location of the first token in s2. The tokens delimiters are commas (,).
ptr = strtok( s2, ",");
Question: 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
void exchange( double *x, double *y )
Question: 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.
int evaluate( int x, int (*poly)( int ))
Question: Write two statements that each initialize character array vowel with the string
of vowels, "AEIOU".
char vowel[] = "AEIOU";
char vowel[] = { 'A', 'E', 'I', 'O', 'U', '\0' };
Question: Find the error in the following program segment. Assume the following
declarations and statements:
int *zPtr; // zPtr will reference array z
int *aPtr = 0;
void *sPtr = 0;
int number;
int z[ 5 ] = { 1, 2, 3, 4, 5 };
++zPtr;
zPtr = z;
++zPtr;
Question: Find the error in the following program segment. Assume the following
declarations and statements:
int *zPtr; // zPtr will reference array z
int *aPtr = 0;
void *sPtr = 0;int number;
int z[ 5 ] = { 1, 2, 3, 4, 5 };
// use pointer to get first value of array
number = zPtr;
number = *zPtr;
Question: Find the error in the following program segment. Assume the following
declarations and statements:
int *zPtr; // zPtr will reference array z
int *aPtr = 0;
void *sPtr = 0;
int number;
int z[ 5 ] = { 1, 2, 3, 4, 5 };
// assign array element 2 (the value 3) to number
number = *zPtr[ 2 ];
number = zPtr[ 2 ];
Question: Find the error in the following program segment. Assume the following
declarations and statements:
int *zPtr; // zPtr will reference array z
int *aPtr = 0;
void *sPtr = 0;
int number;
int z[ 5 ] = { 1, 2, 3, 4, 5 };
// print entire array z
for ( int i = 0; i <= 5; i++ ) cout << zPtr[ i ] << endl;
for ( int i = 0; i < 5; i++ ) cout << zPtr[ i ] << endl;
Question: Find the error in the following program segment. Assume the following
declarations and statements:int *zPtr; // zPtr will reference array z
int *aPtr = 0;
void *sPtr = 0;
int number;
int z[ 5 ] = { 1, 2, 3, 4, 5 };
// assign the value pointed to by sPtr to number
number = *sPtr;
number = *static_cast< int * >( sPtr );
Question: Find the error in the following program segment. Assume the following
declarations and statements:
int *zPtr; // zPtr will reference array z
int *aPtr = 0;
void *sPtr = 0;
int number;
int z[ 5 ] = { 1, 2, 3, 4, 5 };
++z;
++z[4];
Question: Find the error in the following program segment. Assume the following
declarations and statements:
int *zPtr; // zPtr will reference array z
int *aPtr = 0;
void *sPtr = 0;
int number;
int z[ 5 ] = { 1, 2, 3, 4, 5 };
char s[ 10 ];
cout << strncpy( s, "hello", 5 ) << endl;
cout << strncpy( s, "hello", 6 ) << endl;
Question: Find the error in the following program segment. Assume the following
declarations and statements:
int *zPtr; // zPtr will reference array z
int *aPtr = 0;
void *sPtr = 0;
int number;
int z[ 5 ] = { 1, 2, 3, 4, 5 };
char s[ 12 ];
strcpy( s, "Welcome Home");
char s[ 13 ];
strcpy( s, "Welcome Home");
Question: Find the error in the following program segment. Assume the following
declarations and statements:
int *zPtr; // zPtr will reference array z
int *aPtr = 0;
void *sPtr = 0;
int number;
int z[ 5 ] = { 1, 2, 3, 4, 5 };
if ( strcmp( string1, string2 ) )
cout << "The strings are equal" << endl;
if ( strcmp( string1, string2 ) == 0)
cout << "The strings are equal" << endl;
Question: What (if anything) prints when the following statement is performed?Assume
the following variable declarations:char s1[ 50 ] = "jack";
char s2[ 50 ] = "jill";
char s3[ 50 ];
cout << strcpy( s3, s2 ) << endl;
Jill
Question: What (if anything) prints when the following statement is performed?Assume
the following variable declarations:
char s1[ 50 ] = "jack";
char s2[ 50 ] = "jill";
char s3[ 50 ];
cout << strcat( strcat( strcpy( s3, s1 ), " and " ), s2 ) << endl;