Comparison in string literal results in inspecified behavior - C++
I'm using eclipse.
I declared #define OUTPUT_FLAG "-o"
and then, I have the main : int main(int argc, char **a开发者_如何学Gorgv)
after that I write:
for (int i = 1; i < argc; i+=2)
{
if(argv[i]==INPUT_FLAG)
{
cout<<"input flag\n";
input_file=argv[i+1];
}
}
and there I get the error on the subject. Can you help me here? Thank you
You cannot compare strings with ==
in C++. You either have to use strcmp
or convert them to std::string
s and then use the ==
operator. That is, either:
if (!strcmp(argv[i], INPUT_FLAG))
or
if (std::string(argv[i]) == INPUT_FLAG)
You can't compare C strings (char *
) using the ==
operator, as that operator only checks for pointer equality (rather than dereferencing the pointer and comparing each character one by one). Use strcmp()
, or convert the string in argv[]
to a C++ string
type first.
精彩评论