Issue with main arguments handling

后端 未结 3 1033
逝去的感伤
逝去的感伤 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:53

    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.

提交回复
热议问题