Search code examples
cstringpointersequalityequality-operator

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


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?


Solution

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