Advertisement
avr39ripe

pointerPractice

Dec 26th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. void print(const int* begin, const int* end)
  5. {
  6.     while (begin != end)
  7.     {
  8.         std::cout << *(begin++) << ' ';
  9.     }
  10.     std::cout << '\n';
  11. }
  12.  
  13. int main()
  14. {
  15.     int num{ 1 };
  16.     const int arrSize{ 5 };
  17.     int arr[arrSize]{ 1,2,3,4,5 }; //[100(1)][104(2)][108(3)][112(4)][116(5)]// int* const
  18.     bool arrB[arrSize]{ true,true,false,false,true }; //[200][201][202][203][204]
  19.  
  20.  
  21.     print(arr+2, arr + 4);
  22.     int* ptrI{};
  23.     //ptrI = &num;
  24.     //ptrI = new int;
  25.     ptrI = arr + 3; //100 + sizeof(int) * 3 = 100 + 4 * 3 = 112
  26.  
  27.     std::cout << (ptrI != arr) << '\n';
  28.  
  29.     std::cout << (int)arr - (int)ptrI << '\n'; //12 / sizeof(int) = 12 /4 -> 3
  30.  
  31.  
  32.     std::cout << *(ptrI) << '\n'; //112(4)
  33.     *(ptrI - 1) = 333;
  34.     std::cout << *(ptrI - 1) << '\n'; // 112 - sizeof(int) * 1 = 112 - 4 * 1 = 108(3)
  35.     std::cout << *(--ptrI) << '\n'; // 108
  36.  
  37.     std::cout << ptrI[0] << '\n'; // 108 - sizeof(int) * 1 = 108 - 4 * 1 = 104(2)
  38.     std::cout << *ptrI << '\n';
  39.     std::cout << *(ptrI + 0) << '\n';
  40.  
  41.     // arr[i] <=> *(arr + i)
  42.     // ptr[i] <=> *(ptr + i)
  43.  
  44.  
  45.     //ptrI+=1; //116 ptrI = ptrI + 1
  46.   //  ptrI -= 2; // 112 - sizeof(int) * 2 -> 112 - 4 * 2 -> 112 -8 = 104
  47.     return 0;
  48. //    ptrI = new int[5];
  49.  
  50.  
  51.     arr[0] = 333;
  52.  
  53.  
  54.     //print(arr, arrSize);
  55.     //print(arr, arrSize);
  56.    
  57.  
  58.     int val{ 12 };
  59.     val = 45;
  60.  
  61.     const int answer{42};
  62.  
  63.    
  64.     float temp{ 12.0 };
  65.     char symb{ 'a' };
  66.  //   const int* ptrI{ &val };
  67.     int* const cPtrI{ &val };
  68.     const int* const zPtrI{ &answer };
  69.  
  70.     std::cout << zPtrI << '\n';
  71.     std::cout << *zPtrI << '\n';
  72.  
  73.     //*zPtrI = 13;
  74.     //zPtrI = &val;
  75.  
  76.     //*cPtrI = 13;
  77.     //cPtrI = &num;
  78.  
  79.     std::cout << *cPtrI << '\n';
  80.     std::cout << cPtrI << '\n';
  81.  
  82.     //ptrI = &answer;
  83.     //*ptrI = 13;
  84.     std::cout << ptrI << '\n';
  85.  
  86.     ptrI = &val;
  87.  //   *ptrI = 13;
  88.  
  89.     float* ptrF{ &temp };
  90.  
  91.  //   std::cout << *((int*)ptrV) << '\n';
  92.     std::cout << val << '\n';
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement