Issue with main arguments handling

后端 未结 3 1040
逝去的感伤
逝去的感伤 2020-12-12 04:26

I can\'t compare main() arguments with const char* strings.

Simple code for explaining:

#include 

int main         


        
3条回答
  •  盖世英雄少女心
    2020-12-12 04:39

    In this statement

    if(argv[i]=="hello")
    

    you compare pointers because the string literal is implicitly converted to const char * (or char * in C) that points to its first character. As the two pointers have different values the expression is always false. You have to use standard C function strcmp instead. For example

    if( std::strcmp( argv[i], "hello" ) == 0 )
    

    To use this function you should include header (in C++) or (in C).

提交回复
热议问题