String problem, string
1. All strings in C/C ++ end with '\ 0. If you do not care about it, it will cause the string to cross-border, for example:
Char str [10];
Strcpy (str, "0123456789 ");
When copying a string, you must apply for one more byte of Memory Based on the string length to '\ 0'
2. check what the code will output:
Char str1 [] = "Hello ZCM ";
Char str2 [] = "Hello ZCM ";
Char * str3 = "Hello ZCM ";
Char * str4 = "Hello ZCM ";
If (str1 = str2)
{
Cout <"str1 is same as str2 \ n ";
}
Else
{
Cout <"str1 is not same as str2 \ n ";
}
If (str3 = str4)
{
Cout <"str3 is same as str4 \ n ";
}
Else
{
Cout <"str3 is not same as str4 \ n ";
}
Output:
C/C ++ puts the constant string in a separate memory zone, that is, the text constant zone. Other articles will summarize the code segments and properties of c ++,
I will not repeat it here. Str1 and str2 are two string arrays. We will allocate 10 bytes of space for them and copy "Hello ZCM. Institute
This is an array with two different initial addresses.
Str3 and str4 are two pointers. We don't need to allocate memory for it to store string content. We just need to point it to
The memory address is enough. Because "Hello ZCM" is a character constant and has only one copy in the memory, str3 and str4 point to the same address.