我無法比較main()
參數與const char*
字符串。主要參數處理問題
解釋簡單代碼:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
if(argc>1)
{
for (i=1;i<argc;++i)
{
printf("arg[%d] is %s\n",i,argv[i]);
if(argv[i]=="hello")
printf(" arg[%d]==\"hello\"\n",i);
else
printf(" arg[%d]!=\"hello\"\n",i);
}
}
return 0;
}
簡單編譯g++ test.cpp
。當我嘗試執行它時,我看到下一件事:
>./a.out hello my friend
arg[1] is hello
arg[1]!="hello"
arg[2] is my
arg[2]!="hello"
arg[3] is friend
arg[3]!="hello"
我的代碼有什麼問題?
這將在C++中工作過,如果你要使用'標準::矢量 ARGS(的argv,argv的+ ARGC)' - 在C++'的std :: string'確實有一個令人吃驚的'運營商==' –
MSalters