Give char * one name
Char * keeps yourself: I am a string or a pointer? Who can give me a name? First see the following procedures:
Char * getname (int NID) {if (Nid> 0) Return "Name"; else return null;}
Const char * getConstname (int NID) {if (Nid> 0) Return "Name"; else return null;}
Everything seems to be so natural, everything seems to be syndrome. As so than? Try the following test code:
INT Main (int Argc, char * argv []) {// ----------------------------------- ------ String Strname = GetName (1); cout << Strname << endl;
Strname = getname (0); // Run Error Cout << Strname << endl; // ----------------------------- ---- Const string strconstname = getConstname (1); cout << strconstname << endl;
Const string strconstname2 = getConstname (0); // run error cout << strconstname2 << endl; return 0;}
I am dead when I run!
Char * can be used as a string. But during use, sometimes it is used as a pointer. As the two functions returned
Like NULL. Char * is obviously a string, why is it necessary to return each NULL? To get an empty string, use "". Put Return
Null; changed to return ""; This way, char * is consistent.
Maybe you have not used a char * to receive return values in the complaint. Well, see the following:
Char * pstr = getName (0); strcmp (pstr, "");
When it is more, it will still be wrong! I can't always let me compare the string!
How to solve? Very simple: Since it gives char * a string of characters, then let its names are consistent. Don't be empty because it is
The string uses it as a pointer.