The C ++ array type is actually the value type of the exponent group element. For the same array, the Data Types of all its elements are the same, the writing rules of array names should comply with the writing rules of identifiers. I hope this article will give you useful information.
For example, assume that you want to save an integer array and put five integer values. You can declare an array as follows: int myArray [5]; here the compiler matches the memory space shown in Figure 1.7 for the number of components. Since each int needs to be stored in 4 bytes, the entire array occupies 20 bytes of memory.
- 1: #include <iostream.h>
-
- 2: #include <conio.h>
-
- 3: #pragma hdrstop
-
- 4:
-
- 5: int main(int argc,char **argv)
-
- 6: {
-
- 7:char str[]="This is a string.";
-
- 8.cout << str << end1;
-
- 9.str[7]= '\0';
-
- 10. cout << str << end1
-
- 11. cout << end1 << "Press any key to continue...";
-
- 12: getch();
-
- 13: return 0;
-
- 14: }
Further, if you know the number of elements in the array and fill in the array when declaring the array, You can omit the length of the array when declaring the C ++ array. For example: int myArray [] = {-200,-100,200, 0,}; this is feasible, the compiler can determine the number of elements in the array and the memory space allocated to the array from the given value table.
Arrays can be multidimensional. To generate a two-dimensional integer array, 15 int spaces (60 bytes in total) are allocated ). Elements of an array can be the same as those of a one-dimensional array.
Sample access: only two script OPERATOR: int x = mdArray [1] [1] + mdArray [2] [1];
A powerful feature of C ++ is its direct access to memory. Due to this feature, C ++ cannot prevent you from writing to a specific memory address, even if the address is not accessible by the program. The following code is valid, but may cause a program or Windows crash: int array [5]; array [5] = 10; this is a common error.
Because the C ++ array is based on 0, the largest script should be 4 rather than 5. If the end of the array is reloaded, you cannot know which memory is rewritten, making the results unpredictable, and even causing program or Windows crash. This type of problem is difficult to diagnose, because the affected memory usually takes a long time to access, and then the crash occurs, which makes you confused ). Therefore, be careful when writing arrays.
C ++ array rules
◆ The array is based on 0. The first element in the array is 0, the second element is 1, and the third element is 2.
◆ The array length should be a compilation constant. During compilation, the compiler must know how much memory space is allocated to the array. The length of the C ++ array cannot be specified with a variable. The following code is invalid.
◆ Allocate large arrays from stack heap instead of stack (see later ).
◆ You can use variables to specify the array length for arrays allocated from stacks.
This allocates 18 bytes of memory space in the memory to store strings. Based on your comprehension, you may find that the string contains only 17 characters. The reason for 18 bytes allocation is that the string must end with a null termination, and C ++ counts the null termination as one character when allocating memory space. Terminating null is a special character, represented by | 0, which is equal to the value 0. When the program encounters 0 in the character array, it indicates that it has reached the end of the string. To illustrate this, enter and run the following console applications.
- Differences between standard input implementation methods in C and C ++
- How does the C ++ compiler allocate storage space for Const constants?
- Basic Conception and method of C ++ Class Library Design
- Several Methods for converting C ++ Language
- How to better compile C ++ code