In: Computer Science
When answering these questions, please write them in standard English. I asked this question earlier and the answers were all correct, but I had a very hard time understanding what was trying to be said, if you know what I mean.
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;
}
Hey that is a one big list of question.But anyway these are the
answers of some of them.
1. The indirection operator de-references the pointer; allows you
to work with the value the pointer is pointing to.
2. When a * is used with ptr while cout, it means value at. So value at ptr is 7. But when only ptr is used with cout the address store at ptr i.e of 7 will be printed.
3. Uses of *
For multiplication: int a = 2*3 = 6
For pointer definition: int *a;
For indirection: *x= 5, stores 5 at location pointed
4. Addition of integer, Subtraction of integer and Subtracting two pointers of the same type are only 3 operations allowed on pointers.
5. After multiplying 4 by the no. of bytes of an int it adds it to the memory address.
6. It is same as numbers[3] which is 8.
7. New operator are used for Dynamic Memory Allocation to set aside a chunk of memory for a specific data type.
8. Delete operator is used to de-allocate the memory of an object.
9. A pointer should only be returned from a function if it is a pointer to an object that was passed into the function.
10. Constant pointer can never change its address they are pointing to on the other hand pointer to a constant means the one which can not change the value it is pointing to.
I hope these helps.