It's an old-fashioned topic, but you'll make a mistake if you don't pay attention.
Recently in writing a cross-platform printing program, encountered this problem, so remember. Mutual encouragement!
Initializes a string with a char array
#include <string>using std::string;using std::cout;int main (int argc, const char * argv[]) { char charray[] = {' m ', ' A ', ' R ', ' K '}; cout << "Array size =" << sizeof (Charray)/sizeof (char) << Endl; cout << charray << Endl; String str (charray); cout << "str =" << str << "and str ' s size =" << str.length () << Endl;}
For the above code, different compiler output results are inconsistent and random.
Under Mac platform, output results
Array size = 4MARK\310\367\277_\377STR = mark\310\367\277_\377 and str ' s size = 10
To get this result, it is fuck ...
But sometimes the output is the same.
Array size = 4MARKSTR = Mark and str s size = 5
It is obvious that the size of the string is not correct.
Think about it, the original is less ' to ', modify the above code
#include <string>using std::string;using std::cout;int main (int argc, const char * argv[]) { char charray[] = {' m ', ' A ', ' R ', ' K ', ' n '}; cout << "Array size =" << sizeof (Charray)/sizeof (char) << Endl; cout << charray << Endl; String str (charray); cout << "str =" << str << "and str ' s size =" << str.length () << Endl;}
The output is also normal.
Array size = 5MARKSTR = Mark and str s size = 4
That is, the char array initializes the string object, and the array of Terminator ' t ' is not forgotten.
Also, note that the size of the string is smaller than the size of the char array.
Do not know the above things, you have to see?
So the following code output, do you think you will achieve your own expectations?
const char * CP = STR.C_STR (); unsigned long size = Str.length (); Char charray2[size]; for (int i=0; i<size; i++) { charray2[i] = cp[i]; cout << Charray2[i] << Endl; } String str2 (chArray2); cout << "str2 =" << str2 << "and str2 ' s size =" << str2.length () << Endl;
OK, it's obviously wrong here, at least two of them are wrong.
1. Size of the ChArray2
2. ChArray2 's terminator did not add '/'
Modify it to give a complete example
#include <string>using std::string;using std::cout;int main (int argc, const char * argv[]) { //char[]---> St Ring char charray[] = {' m ', ' a ', ' R ', ' K ', ' n '}; cout << "Array size =" << sizeof (Charray)/sizeof (char) << Endl; cout << charray << Endl; String str (charray); cout << "str =" << str << "and str ' s size =" << str.length () << Endl; String--char[] const char * cp = STR.C_STR (); unsigned long size = Str.length (); Char charray2[size + 1]; for (int i=0; i<size; i++) { charray2[i] = cp[i]; cout << Charray2[i] << Endl; } Charray2[size] = ' + '; String str2 (chArray2); cout << "str2 =" << str2 << "and str2 ' s size =" << str2.length () << Endl;}
One might say, why is it not better to construct a string object with a char array than a char * (pointer)?
Yes, it is, but sometimes it takes a char array to manipulate it, and I am also going to share with you some of the problems I have encountered.
In the sample code,
Char charray[] = {' m ', ' a ', ' R ', ' K ', ' n '};
Revision changed to
Char charray[] = "Mark";
Or
const char *charray = "Mark";
The code can also output properly and correctly.
Char Array construction string