Advertisement
avr39-ripe

arrResizeDynMemPointers

Mar 24th, 2020
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. void printArr(int* ptr, int size)
  4. {
  5.     for (auto elP{ ptr }; elP != (ptr + size); ++elP)
  6.     {
  7.         std::cout << *elP << ' ';
  8.     }
  9.     std::cout << '\n';
  10. }
  11.  
  12. void copyArr(int* arr1, int* arr2, int arr2Size)
  13. {
  14.     for (auto elP{ arr2 }; elP != (arr2 + arr2Size); *arr1++ = *elP++);
  15. }
  16.  
  17. int main()
  18. {
  19.     int* ptr{ nullptr };
  20.     int size{ 5 };
  21.     ptr = new int[size]{ 1,2,3,4,5 };
  22.     printArr(ptr, size);
  23.  
  24.     size = 10;
  25.     int* tmp = new int[size]{};
  26.     copyArr(tmp, ptr, 5);
  27.     delete[] ptr;
  28.     ptr = tmp;
  29.     printArr(ptr, size);
  30.  
  31.     size = 4;
  32.     tmp = new int[size] {};
  33.     copyArr(tmp, ptr, 4);
  34.     delete[] ptr;
  35.     ptr = tmp;
  36.     printArr(ptr, size);
  37.  
  38.     delete[] ptr;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement