fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. //PROBLEM 8.8
  7.  
  8. //8.8 (a) Declare an array of type unsigned int called values with five elements, and initialize
  9. //the elements to the even integers from 2 to 10. Assume that the symbolic constant SIZE
  10. //has been defined as 5.
  11.  
  12. unsigned const int SIZE = 5;
  13.  
  14. unsigned int values[SIZE] = { 2, 4, 6, 8, 10 };
  15.  
  16. //8.8 (b) Declare a pointer vPtr that points to an object of type unsigned int.
  17. unsigned int *vPtr;
  18.  
  19. //8.8 (c) Use a for statement to print the elements of array values using array subscript notation.
  20. for (int i = 0; i < SIZE; i++){
  21. cout << values[i] << endl;
  22. }
  23.  
  24. //8.8 (d) Write two separate statements that assign the starting address of array values to pointer
  25. //variable vPtr.
  26. vPtr = values;
  27. vPtr = &values[0];
  28.  
  29. //8.8 (e) Use a for statement to print the elements of array values using pointer/offset notation.
  30. for (int i = 0; i < SIZE; i++){
  31. cout << *(vPtr + i) << endl;
  32. }
  33.  
  34. //8.8 (f) Use a for statement to print the elements of array values using pointer/offset notation
  35. //with the array name as the pointer.
  36. for (int i = 0; i < SIZE; i++){
  37. cout << *(values + i) << endl;
  38. }
  39.  
  40. //8.8 (g) Use a for statement to print the elements of array values by subscripting the pointer to
  41. //the array.
  42. for (int i = 0; i < SIZE; i++){
  43. cout << (vPtr[i]) << endl;
  44. }
  45.  
  46. //8.8 (h) Refer to the fifth element of values using array subscript notation, pointer/offset notation
  47. //with the array name as the pointer, pointer subscript notation and pointer/offset
  48. //notation.
  49. cout << values[4] << endl;
  50. cout << *(vPtr + 4) << endl;
  51. cout << *(values + 4) << endl;
  52. cout << vPtr[4] << endl;
  53.  
  54. //8.8 (i) What address is referenced by vPtr + 3? What value is stored at that location?
  55. //Address: 1002506; 8 is stored;
  56. cout << (vPtr + 3) << endl;
  57. cout << *(vPtr + 3) << endl;
  58.  
  59.  
  60. //8.8 (j) Assuming that vPtr points to values[ 4 ], what address is referenced by vPtr -= 4?
  61. //What value is stored at that location?
  62.  
  63. //Address: 1002500; 2 is stored;
  64.  
  65. unsigned int *temp = vPtr;
  66. cout << (vPtr -= 4) << endl;
  67. cout << *(temp -= 4) << endl;
  68.  
  69.  
  70.  
  71.  
  72. //PROBLEM 8.9
  73.  
  74. //8.9 (a) Declare the variable longPtr to be a pointer to an object of type long.
  75. long *longPtr;
  76.  
  77. //8.9 (b) Assign the address of variable value1 to pointer variable longPtr.
  78. long value1 = 200000;
  79. long value2;
  80. longPtr = &value1;
  81.  
  82. //8.9 (c) Print the value of the object pointed to by longPtr.
  83. cout << *longPtr << endl;
  84.  
  85. //8.9 (d) Assign the value of the object pointed to by longPtr to variable value2.
  86. value2 = *longPtr;
  87.  
  88. //8.9 (e) Print the value of value2.
  89. cout << value2 << endl;
  90.  
  91. //8.9 (f) Print the address of value1.
  92. cout << &value1 << endl;
  93.  
  94. //8.9 (g) Print the address stored in longPtr. Is the value printed the same as value1’s address?
  95. cout << longPtr << endl; //Yes, it's the same.
  96. }
Success #stdin #stdout 0s 4336KB
stdin
Standard input is empty
stdout
2
4
6
8
10
2
4
6
8
10
2
4
6
8
10
2
4
6
8
10
10
10
10
10
0x7ffce1abf39c
8
0x7ffce1abf380
2257752072
200000
200000
0x7ffce1abf388
0x7ffce1abf388