arraynew.cpp 610 B

123456789101112131415161718
  1. // arraynew.cpp -- using the new operator for arrays
  2. #include <iostream>
  3. int main()
  4. {
  5. using namespace std;
  6. double * p3 = new double [3]; // space for 3 doubles
  7. p3[0] = 0.2; // treat p3 like an array name
  8. p3[1] = 0.5;
  9. p3[2] = 0.8;
  10. cout << "p3[1] is " << p3[1] << ".\n";
  11. p3 = p3 + 1; // increment the pointer
  12. cout << "Now p3[0] is " << p3[0] << " and ";
  13. cout << "p3[1] is " << p3[1] << ".\n";
  14. p3 = p3 - 1; // point back to beginning
  15. delete [] p3; // free the memory
  16. // cin.get();
  17. return 0;
  18. }