Using the equality operator == to compare two strings for equality in C

房东的猫 提交于 2019-11-25 23:50:03

问题


int main (int argc, **argv)
{
       if (argv[1] == \"-hello\")
            printf(\"True\\n\");
       else
            printf(\"False\\n\");
}
# ./myProg -hello
False

Why? I realize strcmp(argv[1], \"-hello\") == 0 returns true... but why can\'t I use the equality operator to compare two C strings?


回答1:


Because argv[1] (for instance) is actually a pointer to the string. So all you're doing is comparing pointers.




回答2:


You can't compare strings in C with ==, because the C compiler does not really have a clue about strings beyond a string-literal.

The compiler sees a comparison with a char* on either side, so it does a pointer comparison (which compares the addresses stored in the pointers)




回答3:


In C because, in most contexts, an array "decays into a pointer to its first element".

So, when you have the array "foobar" and use it in most contexts, it decays into a pointer:

if (name == "foobar") /* ... */; /* comparing name with a pointer */

What you want it to compare the contents of the array with something. You can do that manually

if ('p' == *("foobar")) /* ... */; /* false: 'p' != 'f' */
if ('m' == *("foobar"+1)) /* ... */; /* false: 'm' != 'o' */
if ('g' == *("foobar"+2)) /* ... */; /* false: 'g' != 'o' */

or automatically

if (strcmp(name, "foobar")) /* name is not "foobar" */;



回答4:


Because there is no such thing as a C string.

In C, a string is usually an array of char, or a pointer to char (which is nearly the same). Comparing a pointer/array to a const array won't give the expected results.

UPDATE: what I meant by 'no C string' is, there is no string in C. What's usually referred to as a 'C string' is language independent (as 'Pascal string' is), it's the representation of strings as a null-terminated linear array of characters.




回答5:


In C, string values (including string literals) are represented as arrays of char followed by a 0 terminator, and you cannot use the == operator to compare array contents; the language simply doesn't define the operation.

Except when it is the operand of either the sizeof or & operators, or when it is a string literal being used to initialize another array in a declaration, an expression with type "N-element array of T" will have its type implicitly converted (decay) to type "pointer to T", and the value of the expression will be the address of the first element of the array.

So when you write

if (argv[1] == "-hello")

the compiler implicitly converts the expression "-hello" from type "7-element array of char" to "pointer to char" (argv[1] is already a pointer type), and the value of the expression is the address of the character '-'. So what == winds up comparing are two pointer values, which are (most likely) never going to be equal since "-hello" and argv[1] (most likely) occupy different regions in memory.

This is why you have to use library functions like strcmp() to compare string values.




回答6:


Because C strings dont exist as such. They are char arrays ending in a \0.

The equality operator == will test that the pointer to the first element of the array are the same. It wont compare lexicographically.

On the other hand "-hello" == "-hello" may return non zero, but that doesn't mean that the == operator compares lexicographycally. That's due to other facts.

If you want to compare lexicographycally, you can always

#define STR_EQ(s1,s2)    \
   strcmp(s1,s2) == 0

Reading harder I see that you tagged as c++. So you could

 std::string arg1 ( argv[1] );

 if (arg1 == "-hello"){
    // yeahh!!!
 }
 else{
    //awwwww
 }



回答7:


Strings are not native types in C. What you are comparing in that example are two pointers. One to your first argument, and the other is a static character array with the contents of "-hello".

You really want to use strncmp or something similar.




回答8:


When you're using ==, you're comparing pointers. That is, it will return true if the two operands refer to the same string in memory. Therefore, it's unsuitable for use in comparing strings lexicographically.




回答9:


Because C strings are array of characters. Arrays are simply pointers to the first element in the array, and when you compare two pointers using == it compares the memory address they point to, not the values that they point to.



来源:https://stackoverflow.com/questions/3933614/using-the-equality-operator-to-compare-two-strings-for-equality-in-c

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