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

王林
Release: 2020-07-01 09:25:15
Original
3547 people have browsed it

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];
Copy after login

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;
            }
      }
Copy after login
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]
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template