I have written the following code which will does not work but the second snippet will when I change it.
int main( int argc, char *argv[] )
{
if( argv[ 1 ] == "-i" ) //This is what does not work
//Do Something
}
But if I write the code like so this will work.
int main( int argc, char *argv[] )
{
string opti = "-i";
if( argv[ 1 ] == opti ) //This is what does work
//Do Something
}
Is it because the string class has == as an overloaded member and hence can perform this action?
Thanks in advance.
You are correct. Regular values of type
char *do not have overloaded operators. To compare C strings,By comparing the strings the way you did (with
==directly), you are comparing the values of the pointers. Since"-i"is a compile time constant andargv[1]is something else, they will never be equal.