Advertisement
avr39ripe

cppArrPointerConnection

Apr 27th, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     const int arrSize{ 10 };
  6.     int arr[arrSize]{1,2,3,4,5,6,7,8,9,10};
  7.     //[****][****][****][****][****][****][****][****][****][****]
  8.     // arr => 100
  9.     // &arr[0] => 100
  10.     // &arr[1] => 104
  11.  
  12.     // arr + 1 => 100 + 1 => 100 + sizeof(int) * 1 => 100 + 4 * 1 => 104
  13.     // arr + 3 -> 100 + 3 => 100 + sizeof(int) * 3 -> 100 + 4 * 3 => 112
  14.     // &arr[3] => 112 - 2 => 112 - sizeof(int) * 2 -> 112 - 4 * 2 => 104
  15.     std::cout << "arr[0] value:\t" << arr[0] << "\tarr[0] address:\t" << &arr[0] << "\tarr value\t" << arr << '\n';
  16.  
  17.     int num{ 42 };//[42 0 0 0][? ? ? ?]
  18.  
  19.     arr[0] = 123;
  20.     // arr -> &arr[0]
  21.     // *arr -> *(&arr[0]) -> *arr -> arr[0]
  22.     // arr + 1 -> &arr[1]
  23.     // *(arr +1) -> arr[1]
  24.     //
  25.     // *( arr + i ) => arr[i]
  26.     // ( arr + i ) => &arr[i]
  27.  
  28.     int* ptrI{arr};
  29.     ptrI++; // ???
  30.     std::cout << *ptrI << '\n';
  31.     bool* ptrB{};
  32.     double* ptrD{};
  33.  
  34.     std::cout << "ptrI value:\t" << ptrI << "\t*ptrI:\t" << *ptrI << '\n';
  35.  
  36.     *ptrI = 456;
  37.  
  38.     std::cout << "arr[0] value:\t" << arr[0] << "\tarr[0] address:\t" << &arr[0] << "\tarr value\t" << arr << '\n';
  39.  
  40.  
  41.     std::cout << "Is pointers equal? " << ((arr + 1) == ptrI) << '\n';
  42.  
  43.     std::cout << sizeof(int) << ' ' << sizeof(double) << ' ' << sizeof(bool) << '\n';
  44.        
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement