avr39ripe

PV024DynMemBasics

Dec 2nd, 2020 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. int* createInt()
  5. {
  6.     int* ptr{ nullptr };
  7.     ptr = new int{42};
  8.    
  9.     std::cout << "ptr = " << ptr << " *ptr = " << *ptr << '\n';
  10.  
  11.     *ptr = 38;
  12.  
  13.     std::cout << "ptr = " << ptr << " *ptr = " << *ptr << '\n';
  14.  
  15.     return ptr;
  16. }
  17.  
  18.  
  19. int main()
  20. {
  21.     //int num{ 33 };
  22.     int* pointToInt{ nullptr };
  23.    
  24.     pointToInt = createInt();
  25.  
  26.     std::cout << "pointToInt = " << pointToInt << " *pointToInt = " << *pointToInt << '\n';
  27.     ++pointToInt;
  28.     std::cout << "pointToInt = " << pointToInt << " *pointToInt = " << *pointToInt << '\n';
  29.  
  30. //  *pointToInt = 7777;
  31.  
  32.     std::cout << "pointToInt = " << pointToInt << " *pointToInt = " << *pointToInt << '\n';
  33.  
  34.     --pointToInt;
  35.     std::cout << "pointToInt = " << pointToInt << " *pointToInt = " << *pointToInt << '\n';
  36.  
  37.     delete pointToInt;
  38.  
  39.    
  40.     int arrSize{ 5 };
  41.     //std::cin >> arrSize;
  42.     //int arr[arrSize]{ 11,22,33,44,55 };
  43.     //pointToInt = arr;
  44.  
  45.     //pointToInt = new int{ 11 };
  46.     //pointToInt = new int[arrSize]{1,2,3,4,5};
  47.     *pointToInt = 66;
  48.  
  49.     //for (int i{ 0 }; i < arrSize; ++i)
  50.     //{
  51.     //  std::cout << "pointToInt = " << (pointToInt + i) << " *pointToInt = " << *(pointToInt + i) << '\n';
  52.     //}
  53.    
  54.  
  55.     delete pointToInt;
  56.  
  57.     return 0;
  58. }
Add Comment
Please, Sign In to add comment