‣
new
KeywordIn C++, we can create a dynamic array using the new
keyword. The number of items to be allocated is specified within a pair of square brackets. The type name should precede this. The requested number of items will be allocated.
The new
keyword takes the following syntax:
pointer_variable = new data_type;
#include<iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of items:" << "\\n";
cin >> n;
int *arr = new int[n];
cout << "Enter " << n << " items" << endl;
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "You entered: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Enter the number of items:
***7***
Enter 7 items
***27 9 8 12 98 52 34***
You entered: 27 9 8 12 98 52 34