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

user402642 picture user402642 · Oct 14, 2010 · Viewed 73.1k times · Source
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?

Answer

Oliver Charlesworth picture Oliver Charlesworth · Oct 14, 2010

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