Char acstr [] = "AAAAA"; // This is a character array containing only five characters
Char * pcstr = "AAAAA"; // This is a string containing 6 characters, that is, adding an Terminator '\ 0' to five a's'
The output strlen () is 5 in length, Therefore, even if the string is used to obtain the final length, \ 0 is not taken into account:Strlen () calculates the string length, ignoring the terminator \ 0
Char * P = "hello ";
Char * q = "world ";
Char o [] = "hello ";
Cout <"hello" <strlen (p) <Endl; // 5
Cout <"world" <strlen (q) <Endl; // 5
Cout <"O []" <strlen (o) <Endl; // 5
Cout <strlen ("hello") <Endl; // 5
1. The character string actually has more space than the character array to store the terminator \ 0
Char * P = "hello ";
Char * q = "world"; // For example, world actually stores 6 characters, 5 Characters + '\ 0'
But for length, the returned value is still 5: The ending character \ 0 is not considered
Cout <"hello" <strlen (p) <Endl;
Cout <"world" <strlen (q) <Endl;
2. Character arrays do not need to be stored with \ 0 characters. Therefore, the number of characters allocated must be given to strlen (P). + 1 is not required:
A [0] = new char [strlen (p)];
A [1] = new char [strlen (q)];
Both character arrays and character pointer variables can be used to store and operate strings. However, the two are different. Pay attention to the following issues during use:
1. The string pointer variable is a variable used to store the first address of a string. The string itself is stored in a contiguous memory space headed by this first address and ended with '\ 0. Character array is composed of several array elements, which can be used to store the entire string.
2. initialize and assign values to character arrays, which must be of an external or static type, for example, static char st [] = {"C Language"}. This restriction is not imposed on string pointer variables, for example, char * PS = "C Language ";
3. For the string pointer mode char * PS = "C Language"; can be written as: char * pS; PS = "C Language"; for the array mode:
Static char st [] = {"C Language "};
It cannot be written as follows:
Char st [20]; ST = {"C Language "};
You can only assign values to each element of the character array one by one.
From the above points, we can see the difference between the string pointer variable and the character array in use. It can also be seen that it is more convenient to use the pointer variable. As mentioned above, it is dangerous to use a pointer variable before obtaining a definite address, which may cause errors. However, you can assign values to pointer variables directly. This is because the C system must give a definite address when assigning values to pointer variables. Therefore,
Char * PS = "C langage ";
Or char * pS;
PS = "C Language"; is valid.
C ++ provides two String Representation Methods: a c-style string and a string class introduced by standard C ++. We recommend that you use the string class.
1. C-style string
A string is stored in a character array and is usually manipulated by a char * pointer. However, the system is actually stored in a string array, and St points to the first element of the array:
Char * ST = "Hello world ";
The arithmetic operations of common pointers are used to traverse C-style strings. Each pointer is incremented by 1 until the null characters are reached:
While (* st ++ ){...}
When include: # include <cstring. h>
// Return Length
Int strlen (const char *);
// Compare whether the two strings are equal
Int strcmp (const char *, const char *);
// The following two usage methods are unclear:
// Copy the second character to the first character
Char * strcpy (char *, const char *);
// The second character is followed by the first character
Char * strcat (char *, const char *);
The length of a C-style string can be 0 in either of the following ways:
Char * pC1 = 0;
Char * Pc = "";
One testProgram:
# Include <iostream. h>
// Without ". H", eagerly learn to know why
# Include <cstring>
Main (){
// Temp setting avoids chaning the value of P;
// And P can't be constant, for it will be resetted by function strcat ();
Char * P = "Hello world! ", * Temp = P;
Const char * q = "nice to see you! ";
Cout <p <q <Endl;
Cout <* P <"," <* (q + 1) <Endl;
P = temp;
Cout <"P is made by: \ n ";
For (INT I = 0; I <strlen (p); I ++)
Cout <* temp ++ <Endl;
}
2. string type
The C ++ Standard Library supports this.
To use the string class, the header file must be included:
# Include <string>
String ST ("Hello World ");
// Return length (excluding null characters)
St. Size ();
String st2;
// Check whether it is empty
St2.empty () = true or false;
// Copy two strings
String st3 (ST );
St2 = st3; // simple
// Compare whether the values are equal
If (st2 = st3 ){....}
// Connect two strings to the third string
String S1 ("Hello ,");
String S2 ("World !");
String S3 = S1 + S2;
// Or directly add it to S1
S1 + = S2;
// You can convert a C-style string to a string object.
Const char * Pc = "a try ";
String S1 = pc; // OK
// However, reverse conversion cannot be performed automatically
Char * STR = S1; // Error
To implement this conversion, the call name c_str () must be displayed ()
// Almost correct
Char * STR = s1.c _ STR ();
However, to prevent the character array from being directly processed by the program, a pointer to the constant array returned by C-STR (): const char *, STR is defined as a very large pointer, so the violation occurs. Correct:
Const char * STR = s1.c _ STR (); // OK
The string type supports accessing a single character through a subscript operator.
Example:
L const Modifier
For (INT Index = 0; index <512; index ++)
The first question is readability, that is, what is 512? The second problem is maintainability. Multiple modifications may cause problems.
Const int bufsize = 512;
For (INT Index = 0; index <bufsize; index ++)
The const type qualifier provides a solution to convert an object (variable) into a constant (constant ). Any attempt to change this value in the program will lead to compilation errors. Therefore, it is called read-only.
The constant must be initialized after definition, because it cannot be modified.