Advertisement
avr39ripe

cppDynMemBasics

May 6th, 2021
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. void printArr(const int* begin, const int* const end)
  4. {
  5.     while (begin != end)
  6.     {
  7.         std::cout << *begin++ << ' ';
  8.     }
  9.     std::cout << '\n';
  10. }
  11.  
  12. void fillArr(int* begin, const int* const end, int val)
  13. {
  14.     while (begin != end)
  15.     {
  16.         *begin++ = val;
  17.     }
  18. }
  19.  
  20. int globalArr[100];
  21.  
  22. int main()
  23. {
  24.     int num{ 442 };
  25.     int* ptr{ nullptr };
  26.     double* ptrD{ new double{36.6} };
  27.  
  28.     std::cout << "Temperature is: " << *ptrD << '\n';
  29.  
  30.     delete ptrD;
  31.  
  32.     ptr = new int{ 42 };
  33.  
  34.     std::cout << *ptr << '\n';
  35.     *ptr += 3;
  36.     std::cout << *ptr << '\n';
  37.     *ptr = 33;
  38.     std::cout << *ptr << '\n';
  39.    
  40.     delete ptr;
  41.    
  42.     int arrSize;
  43.     std::cout << "Enter array size: ";
  44.     std::cin >> arrSize;
  45.  
  46.     ptr = new int[arrSize] {};
  47.     printArr(ptr, ptr + arrSize);
  48.  
  49.     delete[] ptr;
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement