Petro_zzz

lesson322_21

Oct 6th, 2023
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. char glob = 'G';
  6.  
  7. int sum(int a, int b) {
  8. return a + b;
  9. }
  10.  
  11. void test_address() {
  12. int d = 7;
  13. double x = 3.14;
  14.  
  15. bool arr[]{ true, false, true, false };
  16.  
  17. cout << &d << " " << &x << " "
  18. << arr << " " << sum << endl;
  19.  
  20. cout << &(arr[0]) << endl;
  21. cout << &(arr[3]) << endl;
  22.  
  23. // Взять адрес от rvalue - нельзя
  24. //cout << &(3) << &(sum(1, 2)) << &(x + 1);
  25.  
  26. int* pd = &d;
  27. cout << "int pointer: ";
  28. cout << pd << " " << &d << endl;
  29.  
  30. double* px = &x;
  31. cout << "double pointer: ";
  32. cout << px << " " << &x << endl;
  33.  
  34. cout << "get value: ";
  35. cout << d << " " << *pd << endl;
  36.  
  37. bool* pArr = arr;
  38. bool* pArr2 = &(arr[0]);
  39. cout << *pArr << endl;
  40. cout << *(pArr + 1) << " " << arr[1] << endl;
  41. }
  42.  
  43. void task_pointers_array() {
  44. int arr[]{ 3,4,6,4,2,2 };
  45. for (int k = 0; k < size(arr); k++) {
  46. cout << *(arr + k) << " "
  47. << arr + k << endl;
  48. }
  49.  
  50. for (int* pk = arr; pk != arr + size(arr); pk++) {
  51. cout << *pk << " ";
  52. }
  53. cout << endl;
  54. // arr[k] ~ *(arr + k)
  55.  
  56.  
  57.  
  58. /*
  59. int k = 0;
  60. for (;;) {
  61. k++;
  62. if (k > 5)
  63. break;
  64. }
  65. */
  66. cout << endl;
  67. }
  68.  
  69. void task2() {
  70. int x = 15;
  71. int y = 19;
  72.  
  73. int* px;
  74. px = &x;
  75. int* py = &y;
  76.  
  77. cout << x << " " << px << endl;
  78. cout << y << " " << py << endl;
  79.  
  80. // суммировать адреса безсмысленно
  81. // cout << px + py << endl;
  82. // cout << px * py << endl;
  83. // cout << px / py << endl
  84.  
  85. cout << px - py << endl;
  86. *px = 18; // *px - это как бы x - lvalue
  87. cout << x << endl;
  88. px = py;
  89.  
  90. cout << *px << " "
  91. << *py << " "
  92. << y << endl;
  93. }
  94.  
  95. int main() {
  96. //test_address();
  97. //task_pointers_array();
  98. task2();
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment