How to write the statement declaring a dynamic array: [int size=50;int *p=new int[size];]. Dynamic arrays are defined through the new operator, which is used to dynamically open up space. The size of a dynamic array can be changed dynamically during operation.
Dynamic array:
(Recommended learning: c language tutorial)
We can pass new operator to define dynamic arrays. Because new is used to dynamically open up space, it can of course be used to open up an array space.
Characteristics of dynamic arrays:
The size can be changed dynamically during operation and may not be determined during compilation.
For example:
int size=50; int *p=new int[size];
Let’s give an example:
int main() { using namespace std; int* p = new int[3]; // new运算符返回第一个元素的地址。 p[0] = 10; p[1] = 9; p[2] = 8; for (int i = 0; i < 3; i++) { cout << p[i] << endl; } }
int main(){ int sz = 5; sz = sz + 1; int a[5] = {1, 2, 3, 4, 5}; int *b = new int[sz]; for(int i=0; i<sz; i++) { b[i] = i; std::cout << b[i] << std::endl; } return 0; } 0 1 2 3 4 5 [Finished in 0.2s]
The above is the detailed content of How to write the statement to declare a dynamic array. For more information, please follow other related articles on the PHP Chinese website!