开发者

Comparison in string literal results in inspecified behavior - C++

开发者 https://www.devze.com 2023-04-03 21:22 出处:网络
I\'m using eclipse. I declared #define OUTPUT_FLAG \"-o\" and then, I have the main : int main(int argc, char **a开发者_如何学Gorgv)

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::strings 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消