Issue with main arguments handling

℡╲_俬逩灬. 提交于 2019-11-28 14:34:28

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 <cstring>(in C++) or <string.h> (in C).

Strings can't be compared with ==, use strcmp:

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

You have to #include <string.h>

Whenever you use argv[i] == "hello", the operator "==" donot take string as its operand so in actual the compiler compares the pointer to argv[i] with the pointer to constant string "Hello" which is always false and hence the result you are getting is correct, to compare string literals use srtcmp function. int strcmp(const char *s1, const char *s2); which compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero, if s1 is found, respectively, to be less than, to match, or be greater than s2.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!