C + + Language Basics-character array

Source: Internet
Author: User
Strangely, C + + does not support string variables (variables that place text), and strings in C + + programs are represented by arrays of char data types. For example, you can assign a variable to a char array as follows:
Char text[] = "This is a string.";
This allocates a 18-byte memory space in memory for the string to be stored. Depending on your ability to comprehend, you may find that there are only 17 characters in the string. The reason for allocating 18 bytes is that the string ends with a terminating null, and C + + assigns the terminating null as a character when allocating memory space.
The new term terminating null is a special character, which is equal to the value 0 in |. The program encounters 0 o'clock in the character array, indicating that it is already at the end of the string. To illustrate this point, enter and run the following console application.
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]= ' ";
cout << str << end1
cout << end1 << "Press any key to continue ...";
12:getch ();
13:return 0;
14:}
Parsing initially, the character array contains the string this is a string and a terminating null, which is sent to the screen via cout. The next line assigns the 7th element of the array to |, terminating null. The string is sent to the screen again, but only the this are is displayed. The reason is that the computer thinks that the string in the array terminates on the 7th element, and the remaining string is still in the memory space but not displayed because terminating null was encountered. Figure 1.10 shows an array of characters before and after the statement that assigns the 7th element of the array to |
Before
Thi
Sisastri
ng.\0
After
This
Is\0astri
ng.\0
Figure 1.10 The contents of a character array
Listing 1.6 can also be assigned a value of 0 instead of ' | ', with the same result because the number 0 and char data types ' | ' are equivalent. For example, the following statements are equivalent:
STR[7] = ' | ';
STR[7] = 0;
Indicates that there is a difference between single and double quotes in C + + programs. When assigning values to an array element to terminate null and other character values, you must use single quotes. The single quotation mark turns the character within the quotation mark into an integer value (the ASCII value of the character) and then holds the value in the memory address. When assigning a string to an array of characters, you must use double quotes. If you use the wrong quotation marks, a compilation error occurs for the compiler.

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.