Home >Backend Development >C#.Net Tutorial >How to write the statement to declare a dynamic array

How to write the statement to declare a dynamic array

王林
王林Original
2020-07-01 09:25:153571browse

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.

How to write the statement to declare a dynamic array

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more