Most people do not have a deep understanding of the C ++ array. If you want to assign values to an array, you must first assign values to the array element to terminate null and other character values, therefore, you must carefully process the C ++ array ..
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.
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. You cannot use variables to specify the length of an array. Therefore, the following code is invalid and may cause compilation errors: Be careful not to reload the end of the array.
◆ 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.
Character array
It is strange that C ++ does not support string variables for text placement.) strings in the C ++ array are represented by arrays of char data types. For example, you can assign the variable to the char array as follows. This allocates 18 bytes of memory space in the memory to store strings. Based on your comprehension ability.
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 the C ++ array counts the null termination as one character when allocating memory space. The new term terminate null is a special character, expressed as | 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.
- List 1.6Nulltest.cpp
-
- 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 :}
At the beginning of the analysis, the character array contains the string This is a string and a termination null, which is sent to the screen through cout. In the next row, the first element of the array is assigned a value of | 0, that is, null is terminated. The string is sent to the screen again, but only This is displayed. The reason is that the computer determines that the strings in the array are terminated on 7th elements, and the remaining strings are still in the memory space, but are not displayed because null is terminated. Figure 1.10 demonstrates the character array before and after a statement that assigns 7th elements of an array to | 0.
It indicates that the single quotation marks and double quotation marks in the C ++ program are different. When assigning null or other character values to an array element, you must use single quotes. The single quotation mark is used to convert the character in the quotation mark into the ASCII value of the character in the integer value), and then store the value in the memory address. Double quotation marks must be used to assign a string to a character array. If the quotation marks are used incorrectly, the compiler may encounter a compilation error.
- Introduction to C ++
- Summary Notes on learning and exploring C ++ library functions
- Basic Conception and method of C ++ Class Library Design
- Does C ++ really have market value?
- Basic Conception and method of C ++ Class Library Design