Deck 11: Arrays, Addresses, and Pointers
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/49
Play
Full screen (f)
Deck 11: Arrays, Addresses, and Pointers
1
Pointers, both as variables and function parameters, are used to store addresses.
True
2
Pointers are closely associated with array names.
True
3
Offsets may be included in expressions using pointers.
True
4
If gPtr is a pointer that points to the first element of an integer array (and each integer requires four bytes of storage), *(gPtr + 4 * 4) references the variable that is four integers beyond the variable pointed to by gPtr.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
5
When working with pointers and offsets, the correspondence between the number of bytes and number of variables is not handled by the compiler, so you must perform the conversions yourself.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
6
Any subscript used by a programmer is automatically converted to an equivalent pointer expression by the compiler.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
7
The parentheses in the pointer expression *(gPtr + 3) are not necessary to access the desired array element correctly.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
8
The expression *(gPtr + 3) changes the address stored in gPtr.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
9
A pointer constant is equivalent to a symbolic constant, in that the address stored in the pointer constant cannot be changed once it is set.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
10
Access to an array using a subscript can always be replaced by an equivalent access using the array name as a pointer.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
11
In every respect, an array name and a pointer can be used interchangeably.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
12
The address stored in the array name cannot be changed by an assignment statement.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
13
Attempting to assign the address of an array name is invalid.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
14
A pointer access can sometimes (but not always) be replaced using subscript notation.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
15
One advantage of using subscripts for array processing is that they are more efficient than using pointers directly.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
16
By adding numbers to and subtracting numbers from pointers, we can obtain different addresses.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
17
The addresses in pointers can be compared using any of the relational operators (==, !=, <, >, etc.) that are valid for comparing other variables.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
18
When adding or subtracting numbers to pointers, the computer automatically adjusts the number to ensure that the result still "points to" a value of the original data type.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
19
Addresses cannot be incremented or decremented using prefix or postfix increment and decrement operators.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
20
When initializing pointers you must be careful to set an address in the pointer.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
21
When an array is passed to a function, the array address is the only item actually passed.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
22
If nums is a two-dimensional integer array, *(*(nums + 2) + 1) refers to element nums[1][2].
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
23
The following code is valid in C:
char *message;
strcpy(message,"abcdef");
char *message;
strcpy(message,"abcdef");
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
24
Instead of initially creating a string as an array it is possible to create a string using a pointer.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
25
The declaration char *seasons[4]; creates an array of four elements, where each element is a pointer to a character.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
26
&grade[3] is equivalent to ____; assume that grade is an array of integers, and each integer requires 4 bytes of storage..
A)&grade[0] + 3
B)&grade[0] + 4
C)&grade[0] + (3 * 4)
D)&grade[0] + (3 / 4)
A)&grade[0] + 3
B)&grade[0] + 4
C)&grade[0] + (3 * 4)
D)&grade[0] + (3 / 4)
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
27
The address operator in C is ____.
A)&
B)*
C)->
D))
A)&
B)*
C)->
D))
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
28
The indirection operator in C is ____.
A)&
B)*
C)->
D))
A)&
B)*
C)->
D))
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
29
If we store the address of grade[0] in a pointer named gPtr (using the assignment statement gPtr = &grade[0];), then, the expression ____ references grade[0].
A)gPtr(0)
B)gPtr
C)&gPtr
D)gPtr
A)gPtr(0)
B)gPtr
C)&gPtr
D)gPtr
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
30
The ____ in the expression *(gPtr + 1) is an offset.
A)*
B)gPtr
C)+
D)1
A)*
B)gPtr
C)+
D)1
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
31
If gPtr is a pointer that points to the first element of an integer array (and each integer requires four bytes of storage), ____ references the variable that is three integers beyond the variable pointed to by gPtr.
A)*gPtr + 3
B)*(gPtr + 3)
C)*(gPtr + 3 * 4)
D)*(gPtr + 3 / 4)
A)*gPtr + 3
B)*(gPtr + 3)
C)*(gPtr + 3 * 4)
D)*(gPtr + 3 / 4)
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
32
When working with pointers, the ____ tells the number of variables that are to be skipped over.
A)indirection operator
B)address operator
C)offset
D)address
A)indirection operator
B)address operator
C)offset
D)address
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
33
The expression ____ adds 3 to "the variable pointed to by gPtr."
A)*(gPtr + 3)
B)*gPtr + 3
C)gPtr + 3
D)&gPtr + 3
A)*(gPtr + 3)
B)*gPtr + 3
C)gPtr + 3
D)&gPtr + 3
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
34
When an array is created, the compiler automatically creates an internal ____ for it and stores the base address of the array in it.
A)pointer constant
B)pointer
C)symbolic constant
D)location
A)pointer constant
B)pointer
C)symbolic constant
D)location
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
35
Assuming grade is an array of ten integers, the statement ____ is invalid.
A)grade = &grade[2];
B)*grade = *(grade + 2);
C)*grade = *grade + 2;
D)*grade = *(&grade[2]) + 2;
A)grade = &grade[2];
B)*grade = *(grade + 2);
C)*grade = *grade + 2;
D)*grade = *(&grade[2]) + 2;
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
36
If numPtr is declared as a pointer variable, the expression ____ can also be written as numPtr[i].
A)*numPtr + i
B)(numPtr + i)
C)*numPtr
D)*(numPtr + i)
A)*numPtr + i
B)(numPtr + i)
C)*numPtr
D)*(numPtr + i)
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
37
Pointers ____ be initialized when they are declared.
A)must
B)must not
C)can
D)cannot
A)must
B)must not
C)can
D)cannot
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
38
In performing ____ on pointers, we must be careful to produce addresses that point to something meaningful.
A)comparisons
B)arithmetic
C)subscript operations
D)duplication
A)comparisons
B)arithmetic
C)subscript operations
D)duplication
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
39
Consider the declarations
Int nums[100];
Int *nPtr;
The statement ____ produces the same result as nPtr = nums;.
A)nPtr = &nums[0];
B)nPtr = nums[0];
C)nPtr = *nums[0];
D)nPtr = &nums;
Int nums[100];
Int *nPtr;
The statement ____ produces the same result as nPtr = nums;.
A)nPtr = &nums[0];
B)nPtr = nums[0];
C)nPtr = *nums[0];
D)nPtr = &nums;
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
40
Adding ____ to a pointer causes the pointer to point to the next element of the original data type being pointed to.
A)1
B)1 * sizeof(data type being pointed to)
C)2
D)2 * sizeof(data type being pointed to)
A)1
B)1 * sizeof(data type being pointed to)
C)2
D)2 * sizeof(data type being pointed to)
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
41
Of the following expressions, ____ is the most commonly used. This is because such an expression allows each element in an array to be accessed as the address is "marched along" from the starting address of the array to the address of the last array element.
A)*ptNum--
B)*--ptNum
C)*ptNum++
D)*++ptNum
A)*ptNum--
B)*--ptNum
C)*ptNum++
D)*++ptNum
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
42
int *ptNum = &miles; is ____.
A)always valid
B)never valid
C)only valid if miles is declared as an integer variable before ptNum is declared
D)only valid if miles is declared as an array of integers before ptNum is declared
A)always valid
B)never valid
C)only valid if miles is declared as an integer variable before ptNum is declared
D)only valid if miles is declared as an array of integers before ptNum is declared
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
43
Consider the following declarations of a function that receives an array of integers and finds the element with the maximum value:
(i) findMax(int *vals, int numEls)
(ii) findMax(int vals[], int numEls)
The address in vals may be modified ____.
A)only if the function is declared as in (i)
B)only if the function is declared as in (ii)
C)if either (i) or (ii) is used
D)in neither case because an array variable cannot be modified (it is a pointer constant)
(i) findMax(int *vals, int numEls)
(ii) findMax(int vals[], int numEls)
The address in vals may be modified ____.
A)only if the function is declared as in (i)
B)only if the function is declared as in (ii)
C)if either (i) or (ii) is used
D)in neither case because an array variable cannot be modified (it is a pointer constant)
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
44
If nums is a two-dimensional integer array, ____ refers to element nums[0][0].
A)*nums
B)*(*nums)
C)*(&nums)
D)&(*nums)
A)*nums
B)*(*nums)
C)*(&nums)
D)&(*nums)
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
45
If nums is a two-dimensional integer array, ____ refers to element nums[1][0].
A)*nums[1]
B)*nums[0]
C)*nums + 1
D)*nums++
A)*nums[1]
B)*nums[0]
C)*nums + 1
D)*nums++
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
46
A suitable equivalent to the function header calc(int pt[2][3]) is ____.
A)calc(int *(*pt))
B)calc(int (*pt)[])
C)calc(int (*pt)[2])
D)calc(int (*pt)[3])
A)calc(int *(*pt))
B)calc(int (*pt)[])
C)calc(int (*pt)[2])
D)calc(int (*pt)[3])
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
47
The header line ____ declares calc to be a pointer to a function that returns an integer.
A)int *calc()
B)int (*calc)()
C)int &calc()
D)int calc(*)
A)int *calc()
B)int (*calc)()
C)int &calc()
D)int calc(*)
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
48
You can replace lines 5 and 6 in the following function with ____.
1 /* copy string2 to string1 */
2 void strcopy(char string1[], char string2[])
3 {
4 int i = 0;
5 while (string1[i] = string2[i])
6 i++;
7 }
A) while (*string1 = *string2) ;
B) while (*string1 = string2) ;
C) while (*string1++ = *string2++) ;
D) while (*++string1 = *++string2) ;
1 /* copy string2 to string1 */
2 void strcopy(char string1[], char string2[])
3 {
4 int i = 0;
5 while (string1[i] = string2[i])
6 i++;
7 }
A) while (*string1 = *string2) ;
B) while (*string1 = string2) ;
C) while (*string1++ = *string2++) ;
D) while (*++string1 = *++string2) ;
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
49
After creating two variables as follows:
Char message1[81] = "this is a string";
Char *message2 = "this is a string";
The statement ____ is not valid in C.
A) message1 = "A new message";
C) message2 = message1;
B) message2 = "A new message";
D) message2[0] = 'T';
Char message1[81] = "this is a string";
Char *message2 = "this is a string";
The statement ____ is not valid in C.
A) message1 = "A new message";
C) message2 = message1;
B) message2 = "A new message";
D) message2[0] = 'T';
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck