Array definition and initialization in C++

Definition and Initialization of Arrays in C++

One-dimensional array:

1. Static int array[100]. Defines the array array, and does not initialize the array

2. Static int array[100], defines and initializes the array array

3. Dynamic int* array = new int [100], allocate memory on the heap, use the new keyword to allocate memory, the return value is a pointer, so the array is declared as int* type. Need to call delete function

To delete: delete[] array; an array array of length 100 is allocated and deleted.

4. Dynamic in* array=new int[100](1,2);delete array; an array array of length 100 is allocated and the first two elements are initialized

Two, two-dimensional array:

1. The array is statically int array[10][10]defined and not initialized

2. The static int array[10][10]={{1,2},{2,3}}array is initialized with array[0][0,1] and array[1][0,1]

3. Dynamic int (*array)[n]=new int [m][n]; delete array The size of the second-dimensional array must be specified here

4. Dynamic int** array = new int*[m]; The size of the first-dimensional array must be specified here, and the size of the second-dimensional array can be dynamically applied through the new keyword. The size of the first dimension of the dimension of the two-dimensional array must be specified, and the second dimension can continue to be dynamically allocated, but it needs to call the delete destructor to release the memory, for example:

int (*array)[n]=new int[m][n];
int **array=new int*[m];
//对其进行初始化
for(int i =0;i<m;i++){
array[i]=new int[n];
}
//进行销毁
先销毁第二维的数组
forint i =0;i<m;i++){
delete[] array[i];
}
再销毁第一维的数组
delete[] array;

3. Multidimensional array

int* array = new int[m][3][4]; Only the first dimension can be a variable, other dimensions must be constant, otherwise an error will be reported
delete []array; Memory must be released, otherwise memory will leak

Section:

When using new for dynamic allocation, for multidimensional arrays, you need to specify

4. Arrays are passed as function parameters

One-dimensional array passing:
  void func(int* array);
  void func(int array[]);    
two-dimensional array passing:
  void func(int** array);
  void func(int (*array)[n]);                  
array When the name is used as a function parameter, in the function body, it loses its own connotation, it is just a pointer, and when it loses its connotation, it also loses its constant characteristics, which can be used for self-increment, self-decrement and other operations. can be modified.

Five, character array

Arrays of type char are called character arrays, and the last bit in the character array is the transfer character '\0' (also known as a null character), which indicates that the string has ended. The string class is defined in C++ and the Cstring class is defined in Visual C++.

Each character in the string occupies one byte, plus a final null character. Such as:

char array [10] = “cnblogs”;

Although only 7 bytes, the string length is 8 bytes.

You can also not define the length of the string, such as:

char array[] = “abc”;

Reference blog post:

Array definition and initialization in C++

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326035973&siteId=291194637