Advertisement
KuoHsiangYu

C++動態建立陣列示範

May 15th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. /* Windows 10 作業系統電腦 */
  2.  
  3. #include <iostream>
  4. #include <cstdlib>
  5.  
  6. using std::cout;
  7. using std::cin;
  8.  
  9. int main(int argc, char *argv[]) {
  10.     system("color f0");
  11.  
  12.     int length = 0;
  13.     int *ptr = NULL;
  14.  
  15.     cout << "Please input array length : ";
  16.     cin >> length;
  17.     ptr = new int[length];
  18.  
  19.     cout << "ptr = " << ptr << "\n";
  20.  
  21.     for (int i = 0; i < length; i++) {
  22.         *(ptr + i) = (i + 1);
  23.     }
  24.  
  25.     for (int i = 0; i < length; i++) {
  26.         cout << ptr[i] << ", ";
  27.     }
  28.     cout << "\n";
  29.  
  30.     delete[] ptr;
  31.     ptr = NULL;
  32.  
  33.     cout << "\n";
  34.     system("pause");
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement