fork download
  1. #include <iostream>
  2.  
  3. int* copy_array(const int* arr, std::size_t size) // const because we don't intend to modify the contents of arr
  4. {
  5. int* copy = new int[size];
  6. for (std::size_t i=0; i<size; ++i)
  7. copy[i] = arr[i];
  8.  
  9. return copy;
  10. }
  11.  
  12. void print(const int* arr, std::size_t size)
  13. {
  14. for (std::size_t i=0; i<size; ++i)
  15. std::cout << arr[i] << ' ' ;
  16. std::cout << '\n' ;
  17. }
  18.  
  19. int main()
  20. {
  21. const std::size_t size = 8 ;
  22. int array1[size] = { 5, 12, -2, 8, 47, 12, 81, 0 };
  23.  
  24. int * array1_copy = copy_array(array1, size) ;
  25.  
  26. print(array1, size);
  27. print(array1_copy, size);
  28.  
  29. delete [] array1_copy;
  30. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
5 12 -2 8 47 12 81 0 
5 12 -2 8 47 12 81 0