I have a problem with passing an argument into my program, seems to not be equal to what I put in as argument, except they're identical. Turning them into a string makes them identical, but I would like to know why the initial duo isn't.
Here's my code:
int main(int argc, char *argv[]) {
if (argc>1) {
cout << "#" << argv[1] << "#" << endl;
cout << "#" << "nomast" << "#" << endl;
cout << (argv[1] == "nomast" ? "equal" : "not equal") << endl;
string s1 = argv[1];
string s2 = "nomast";
cout << (s1 == s2 ? "equal after all" : "nope") << endl;
system("pause");
}
return 0;
}
When I launch the compiled code with "call thingy.exe nomast" I get the output
#nomast#
#nomast#
not equal
equal after all
Press any key to continue . . .
My best idea is that I'm not handling the "char *argv[]" properly. Don't know how to handle it differently though.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…