In C++,
One, allocate a dynamic array for it:
Format: type name * pointer variable name = new type name [number of elements];
(The number of elements can be a variable)
eg:
int i=5
int *p=new int[i];
two. Release the array for it: (When the number of elements in the declared array is dynamic, after the end of use, the space needs to be released)
Format: delete[] pointer variable name
Three, example: use NEW to apply for a dynamic array
#include<iostream.h>
int main()
{
int num=0;
cout<<"请输入数组的个数"<<endl;
cin>>num;
cout<<"请依次输入"<<num<<"个整数"<<endl;
int*pSz=new int[num];
for(int i=0;i<num;i++)
cin>>pSz[i];
for(i=0;i<num;i++)
cout<<"第"<<i<<"个数为"<<pSz[i]<<endl;
delete[] pSz;
return 0;
}
The result is as follows