C + + character array input and output and string end flags used to explain _c language

Source: Internet
Author: User
Tags array length

Input and output of C + + character array
The input and output of a character array can be two ways:
1) input and output characters.

2 input or output the whole string one at a time. For example, the following program segments are available:

  Char str[20];
  cin>>str; Input string with character array name
  cout<<str;//output string with character array name


Enter a string at run time, such as:

  China↙


In memory, the state of array str, as shown in Figure 5.9, automatically adds a terminator ′\0′ after 5 characters.


Output, the output character is ′\0′ until the Terminator is terminated. The output results are:

  The


As mentioned earlier, character array name Str represents the address of the first element of a character array, and executes "cout<<str;" process is to output characters from the first element of the array pointed to by Str until the ′\0′ is encountered.

Note the following points about the input and output of character arrays:
The output character does not include the Terminator ′\0′.
When you output a string, the cout stream uses the character array name, not the array element name.
If the length of the array is greater than the actual length of the string, output only to the ′\0′ end.
If a character array contains more than one ′\0′, the output ends when the first ′\0′ is encountered.
When you enter a string from the keyboard to the computer with CIN, the string entered from the keyboard should be shorter than the length of the defined character array, or there will be a problem.

C + + provides a getline function in the CIN stream for reading a line of characters (or a number of characters before a line), and is safe and convenient to use.


C + + string and string end flags

An array of characters can hold characters in a string. Such as:

  Char str[12]={' I ', ', ' a ', ' m ', ', ' h ', ' A ', ' P ', ' P ', ' Y '};


Uses a one-dimensional character array, str, to hold a character in a string ″i am Happy″. The actual length of the string (10) is not equal to the length of the array (12), and the system automatically fills the null character ′\0′ for the last two elements of the character array, after storing 10 characters above.

To determine the actual length of the string, C + + prescribes a "string end flag", represented by the character ′\0′. In the above array, the 11th character is ′\0′, indicating that the valid character of the string is the preceding 10 characters. In other words, encountering a character ′\0′ means that the string ends here, and the character in front of it makes up a string.

For a string constant, the system automatically appends all characters with a ′\0′ as a terminator. For example, the string ″i am Happy″ has a total of 10 characters, but in memory it occupies 11 bytes, and the last byte ′\0′ is automatically added by the system.

In a program, you often rely on detecting the location of the ′\0′ to determine whether the string ends, rather than determining the length of the string based on the length of the array. Of course, when defining a character array, you should estimate the actual string length to ensure that the array length is always greater than the actual length of the string. If you have multiple strings of different lengths in one character array, you should make the array longer than the length of the longest string.

Description: ′\0′ is just a sign for identification.

If you output a string with the following statement:

  Cout<<″ you
  Do?″;


When the system outputs characters one by one when executing this statement, how does it stop when it determines which character should be exported?

Another way to initialize a character array is to initialize the character array with string constants. For example:

  Char str[]={″i am Happy″};


You can also omit curly braces and write directly

  Char str[]=″i am Happy″;


Instead of using a single character as the initial value, we use a string (note that the ends of the string are enclosed by a double apostrophe rather than a single apostrophe) as the initial value. Obviously, this method is intuitive, convenient and consistent with people's habits. Note: The length of the array str is not 10, but 11 (since the end of the string constant is the system plus a ′\0′). Therefore, the initialization above is equivalent to the following initialization:

  Char str[]={' I ', ', ' a ', ' m ', ', ', ' h ', ' A ', ' P ', ' P ', ' Y ', ' n '};


And not the following equivalence:

  Char str[]={' I ', ', ' a ', ' m ', ', ' h ', ' A ', ' P ', ' P ', ' Y '};


The length of the former is 11, the length of which is 10. If there is

  Char Str[10]=″china″;


The first 5 elements of the array str are ′c′,′h′,′i′,′n′,′a′, the 6th element is ′\0′, and the last 4 elements are null characters. See figure.

It should be explained that the character array does not require its last character to be ′\0′ or even contain ′\0′. It is entirely lawful to write as follows:

  Char str[5]={' C ', ' h ', ' I ', ' n ', ' a '};


Whether you need to add ′\0′ and decide exactly what you need to do. But because the C + + compilation system automatically adds a ′\0′ to the string constants. Therefore, people in order to make the processing method consistent, easy to determine the actual length of strings, as well as the corresponding processing in the program, in the character array after the valid characters are also artificially added a ′\0′. Such as:

  Char str [6]={' C ', ' h ', ' I ', ' n ', ' A ', ' a ', '; '};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.