In: Computer Science
Chapter 9 (Pointers) – Review Questions
Short Answer
1. What does the indirection operator do?
2. Look at the following code.
int x = 7;
int *ptr = &x;
What will be displayed if you send the expression *ptr to cout? What happens if you send the expression ptr to cout?
3. So far you have learned three different uses for the * operator. What are they?
4. What math operations are allowed on pointers?
5. Assuming ptr is a pointer to an int, what happens when you add 4 to ptr?
6. Look at the following array definition.
int numbers[] = {2, 4, 6, 8, 10};
What will the following statement display?
cout << *(numbers + 3) << endl;
7. What is the purpose of the new operator?
8. What is the purpose of the delete operator?
9. Under what circumstances can you successfully return a pointer from a function?
10. What is the difference between a pointer to a constant and a constant pointer?
11. What are two advantages of declaring a pointer parameter as a constant pointer?
Fill-in-the-Blank
12. Each byte of memory is assigned a unique ___________.
13. The _________ operator can be used to determine a variable’s address.
14. ____________variables are designed to hold addresses.
15. The ___________ operator can be used to work with the variable a pointer points to.
16. Array names can be used as _________, and vice versa.
17. Creating variables while a program is running is called ____________.
18. The ____________ operator is used to dynamically allocate memory.
19. A pointer that contains the address 0 is called a(n) ________ pointer.
20. When a program is finished with a chunk of dynamically allocated memory, it should free it with the __________ operator.
21. You should only use pointers with delete that were previously used with __________.
Algorithm Workbench
22. Look at the following code:
double value = 29.7;
double *ptr = &value;
Write a cout statement that uses the ptr variable to display the contents of the value variable.
23. Look at the following array definition:
int set[10];
Write a statement using pointer notation that stores the value 99 in set[7]
24. Write a code that dynamically allocates an array of 20 integers, then uses a loop to allow the user to enter values for each element of the array.
25. Assume tempNumbers is a pointer that points to a dynamically allocated array. Write code that releases the memory used by the array.
26. Look at the following function definition:
void getNumber(int &n)
{
cout << “Enter a number: “;
cin >> n;
}
In this function, the parameter n is a reference variable. Rewrite the function so n is a pointer.
27. Write the definition of ptr, a pointer to a constant int.
28. Write the definition of ptr, a constant pointer to an int.
True or False
29. Each byte of memory is assigned a unique address. T F
30. The * operator is used to get the address of a variable. T F
31. Pointer variables are designed to hold addresses. T F
32. The & symbol is called the indirection operator. T F
33. The & operator dereferences a pointer. T F
34. When the indirection operator is used with a pointer variable, you are actually working with the value the pointer is pointing to. T F
35. Array names cannot be dereferenced with the indirection operator. T F
36. When you add a value to a pointer, you are actually adding that number times the size of the data type referenced by the pointer. T F
37. The address operator is not needed to assign an array’s address to a pointer. T F
38. You can change the address that an array name points to. T F
39. Any mathematical operation, including multiplication and division, may be performed on a pointer. T F
40. Pointers may be compared using the relational operators. T F
41. When used as function parameters, reference variables are much easier to work with than pointers. T F
42. The new operator dynamically allocates memory. T F
43. A pointer variable that has not been initialized is called a null pointer. T F
44. The address 0 is generally considered unusable. T F
45. In using a pointer with the delete operator, it is not necessary for the pointer to have been previously used with the new operator. T F
Find the Error
Each of the following definitions and program segments has errors. Locate as many as you can.
46. int ptr* = nullptr;
47. int x, *ptr = nullptr;
&x = ptr;
48. int x, *ptr = nullptr;
*ptr = &x;
49. int x, *ptr = nullptr;
ptr = &x;
ptr = 100; //Store 100 in x
cout << x << endl;
50. int numbers[] = {10, 20, 30, 40, 50};
cout << “The third element in the array is ”;
cout << *numbers + 3 << endl;
51. int values[20], *iptr = nullptr;
iptr = values;
iptr *= 2;
52. float level;
int fptr = &level;
53. int *iptr = &ivalue;
int ivalue;
54. void doubleVal(int val)
{
*val *= 2;
}
55. int *pint = nullptr;
new pint;
56. int *pint = nullptr;
pint = new int;
if (pint == nullptr)
*pint = 100;
else
cout << “Memory allocation error\n”;
57. int *pint = nullptr;
pint = new int[100]; //Allocate memory
.
.
Code that process the array.
.
.
delete pint; // Free memory
58. int *getNum()
{
int wholeNum;
cout << “Enter a number: “;
cin >> wholeNum;
return &wholeNum;
}
59. const int arr[] = {1, 2, 3};
int *ptr = arr;
60. void doSomething(int * const ptr)
{
int localArray[] = {1, 2, 3};
ptr = localArray;
}
1. What does the indirection operator do?
Answer: Indirection operator is used to point the value of pointer.
2. Look at the following code.
int x = 7;
int *ptr = &x;
What will be displayed if you send the expression *ptr to cout? What happens if you send the expression ptr to cout?
Ans: The value 7 will be displayed if the expression *ptr is sent to cout. If the expression ptr is sent to cout, the address of the variable x will be displayed.
3. So far you have learned three different uses for the * operator. What are they?
Ans: Defintion of pointer variable for ex int *a, multiplication for ex a*b, and indirection operator for ex *a=3
4. What math operations are allowed on pointers?
Ans: Addition and subtraction of an integer to a pointer.
5. Assuming ptr is a pointer to an int, what happens when you add 4 to ptr?
Ans: Here, we add the four times the integer size to the given value of the address.
6. Look at the following array definition.
int numbers[] = {2, 4, 6, 8, 10};
What will the following statement display?
cout << *(numbers + 3) << endl;
Ans: From base move to the third value. Hence 8
7. What is the purpose of the new operator?
Ans: It allocates the memory dynamically.
8. What is the purpose of the delete operator?
Ans: Free the memory that is allocated with new operator.
9. Under what circumstances can you successfully return a pointer from a function?
Ans: case 1: When the pointer is an object in the argument section of the function
case 2: A pointer to the dynamically allocated object
10. What is the difference between a pointer to a constant and a constant pointer?
Ans:
Pointer to a constant: The pointer points to the constant value which cannot be altered.
Constant Pointer: This is with reference to the pointer having constant address which cannot be changed.
11. What are two advantages of declaring a pointer parameter as a constant pointer?
Ans:
1. Cannot be altered to point to any other value as given in above question
2. Accepts the address of both normal and constant values.
3. Has the ability to change the value of the variable, but not the pointer address.
Fill-in-the-Blank
12. Each byte of memory is assigned a unique ___________.
Ans: Address (Since it should be unique)
13. The _________ operator can be used to determine a variable’s address.
Ans: & (defined by the language)
14. ____________variables are designed to hold addresses.
Ans:
Pointer (from the definition of the pointer)
15. The ___________ operator can be used to work with the variable a pointer points to.
Ans: * (Given from the definition of the pointer operator)
16. Array names can be used as _________, and vice versa.
Ans: Pointers
17. Creating variables while a program is running is called ____________.
Ans: dynamic memory allocation (while running means dynamic and creating variables means allocating memory)
18. The ____________ operator is used to dynamically allocate memory.
Ans: New (Given in the definition of new operator)
19. A pointer that contains the address 0 is called an ________ pointer.
Ans: NULL (0 means empty)
20. When a program is finished with a chunk of dynamically allocated memory, it should free it with the __________ operator.
Ans: delete
21. You should only use pointers with delete that were previously used with __________.
Ans: new
Algorithm Workbench
22. Look at the following code:
double value = 29.7;
double *ptr = &value;
Write a cout statement that uses the ptr variable to display the contents of the value variable.
Ans: cout<<*ptr; //as we know that indirection operator gives the value.
23. Look at the following array definition:
int set[10];
Write a statement using pointer notation that stores the value 99 in set[7]
Ans: Since set is an array.
*(set+7)=99
24. Write a code that dynamically allocates an array of 20 integers, then uses a loop to allow the user to enter values for each element of the array.
Ans:
int *ptrvariable;
ptrvariable = new int[20]; //dynamic allocation of 20 integers
for (int i = 0; i < 20; i++)
{
cout > ptrvariable[i];
}
25. Assume tempNumbers is a pointer that points to a dynamically allocated array. Write code that releases the memory used by the array.
Ans:
delete[] tempNumbers;
26. Look at the following function definition:
void getNumber(int &n)
{
cout << “Enter a number: “;
cin >> n;
}
In this function, the parameter n is a reference variable. Rewrite the function so n is a pointer.
Ans: Change the reference variable to the pointer.
void getNumber(int *n)
{
cout << “Enter a number: “;
cin >> *n;
}
27. Write the definition of ptr, a pointer to a constant int.
Ans:
28. Write the definition of ptr, a constant pointer to an int.
True or False
29. Each byte of memory is assigned a unique address. T F
Ans: True
30. The * operator is used to get the address of a variable. T F
Ans: False
31. Pointer variables are designed to hold addresses. T F
Ans: True (From the pointer defintion)
32. The & symbol is called the indirection operator. T F
Ans: False (& is address operator)
33. The & operator dereferences a pointer. T F
Ans: False (Since * is used)
34. When the indirection operator is used with a pointer variable, you are actually working with the value the pointer is pointing to. T F
Ans: True (Since given the definition of the pointer)
35. Array names cannot be dereferenced with the indirection operator. T F
Ans: False (Array names means constant pointers with the first value pointing. If pointers are used as reference so as array names)
36. When you add a value to a pointer, you are actually adding that number times the size of the data type referenced by the pointer. T F
Ans: True (When we add an integer to a pointer means we are going to add the size of that particular data type)
37. The address operator is not needed to assign an array’s address to a pointer. T F
Ans: True (We use the * operator)
38. You can change the address that an array name points to. T F
Ans: False (It is allocated and fixed. Array names are constant pointers pointing to the starting value of array.
39. Any mathematical operation, including multiplication and division, may be performed on a pointer. T F
Ans: False (only addition and subtraction with increments and decrement operations)
40. Pointers may be compared using the relational operators. T F
Ans: True (yes, all the relational operators can be used for comparision)
41. When used as function parameters, reference variables are much easier to work with than pointers. T F
Ans: True (Because reference variables directly works with memory address)
42. The new operator dynamically allocates memory. T F
Ans: True (definition of new operator)
43. A pointer variable that has not been initialized is called a null pointer. T F
Ans: False (definition of null pointer contains address 0)
44. The address 0 is generally considered unusable. T F
Ans: True (since it points to the empty memory space)
45. In using a pointer with the delete operator, it is not necessary for the pointer to have been previously used with the new operator. T F
Find the Error
Each of the following definitions and program segments has errors. Locate as many as you can.
46. int ptr* = nullptr;
Ans: The variable should be declared as int *ptr.
47. int x, *ptr = nullptr;
&x = ptr;
Ans: The address of the variable cannot point to the pointer variable. So, we have ptr=&x
48. int x, *ptr = nullptr;
*ptr = &x;
Ans: *ptr refers to the value of the variable. Otherwise initialize some value. Hence we have ptr=&x.
49. int x, *ptr = nullptr;
ptr = &x;
ptr = 100; //Store 100 in x
cout << x << endl;
Ans: Storing 100 in ptr is *ptr=100 (because * operator refers the value of the variable)
50. int numbers[] = {10, 20, 30, 40, 50};
cout << “The third element in the array is ”;
cout << *numbers + 3 << endl;
Ans: The array should be refered as *(arrayname+element position). Hence cout<<*(numbers+3)<<endl;
51. int values[20], *iptr = nullptr;
iptr = values;
iptr *= 2;
Ans:Here, iptr is pointer. So, we cannot perform pointer multiplication.
52. float level;
int fptr = &level;
Ans: Given the value as float, but considered integer pointer.
53. int *iptr = &ivalue;
int ivalue;
Ans: iptr cannot be initialized with the address of ivalue. ivalue is defined after iptr.
54. void doubleVal(int val)
{
*val *= 2;
}
Ans: * operator can be used as pointer parameter and hence the value will be doubled.
void doubleVal(int *val)
{
*val *= 2;
}
The second option is give the parameter as reference variable and change the pointer to the variable
void doubleVal(int &val)
{
val *= 2;
}
55. int *pint = nullptr;
new pint;
Ans: new should be declared as pint = new int. Because new should be followed with the datatype.
56. int *pint = nullptr;
pint = new int;
if (pint == nullptr)
*pint = 100;
else
cout << “Memory allocation error\n”;
Ans:
It is clear that wheneve pint points to the nullptr, it means no memory and hence memory allocation error.
int *pint;
pint = new int;
if (pint == NULL)
cout << "Memory allocation error\n";
else
*pint = 100;
57. int *pint = nullptr;
pint = new int[100]; //Allocate memory
Code that process the array.
delete pint; // Free memory
Ans: There is no array in the last line and so there is confusion to delete the element in which position. Brackets are used to indicate the entire array. So give it as delete [] pint.
58. int *getNum()
{
int wholeNum;
cout << “Enter a number: “;
cin >> wholeNum;
return &wholeNum;
}
Ans: The statement above has an error. The reason is that the function needs to pass wholeNum as a parameter. The problem is that in this function we return the address of a variable which is deleted once the function work is over. To return the address properly, one should dynamically allocate memory through the use of the new operator or pass the variable as aparameter like int *getNum(int wholeNum);
59. const int arr[] = {1, 2, 3};
int *ptr = arr;
Ans:
Constant pointer should not point to any other location. So remove the constant keyword and
int arr[] = {1, 2, 3};
int *ptr = arr;
60. void doSomething(int * const ptr)
{
int localArray[] = {1, 2, 3};
ptr = localArray;
}
Ans: The above reason can be given here. The parameter is constant pointer. But we have pointed it to different address. So, remove the keyword const in parameter.
void doSomething(int * ptr)
{
int localArray[] = {1, 2, 3};
ptr = localArray;
}