tags:

views:

252

answers:

9
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?

+8  A: 

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

Oli Charlesworth
+6  A: 

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.

jv42
There is definitely a thing called a [C string](http://en.wikipedia.org/wiki/C_string). I don't know exactly what you mean. Perhaps that "There is no C string-type in the C language"?
Magnus Hoff
That's just abusing the term. What's described there is a null-terminated string, as opposed to a 'Pascal String', which gives the size as the first byte.
jv42
Now, the C programming language (as the vanilla C++ without STL) doesn't have a string type. There is a compiler feature that automatically converts text between double quotes "..." to a constant char array (which is null terminated), leading to this very common mistake when handling strings in C/C++.
jv42
There are also string library functions, which operate on null-terminated arrays of `char`.
David Thornley
More properly, a string value is *represented* by a sequence of characters followed by a 0 terminator. These sequences are stored as arrays of `char` (string *literals* are stored as arrays of `char` in C, `const char` in C++).
John Bode
+2  A: 

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
 }
Tom
Someone edited the tag to C++, which is wrong. It's back to C now
Carlo del Mundo
A: 

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.

Shynthriir
[Arrays are not just pointers](http://publications.gbdirect.co.uk/c_book/chapter5/arrays_and_address_of.html), sheesh.
detly
@detly: Easy mistake to make, considering that arrays decay into pointers on almost any excuse.
David Thornley
@David Thornley: Even so, it's best to get the terminology correct. C's confusing enough without getting the basics wrong.
John Bode
My wording was a little harsh, sorry. But still, it's not. There are times when that assumption will really bite you.
detly
+1  A: 

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.

kanaka
+5  A: 

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)

Bart van Ingen Schenau
A: 

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.

waffle paradox
+2  A: 

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")) /* ... */;
pmg
It is in C. Someone edited my post to reflect C++ (though I could understand that I was using C++ constructs) Sorry for the confusion
Carlo del Mundo
A: 

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.

John Bode