LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   C Arguments to main - can't compare values (http://www.linuxquestions.org/questions/programming-9/c-arguments-to-main-cant-compare-values-893523/)

madsovenielsen 07-24-2011 06:42 PM

C Arguments to main - can't compare values
 
Whats wrong with this:
Code:

#include <stdio.h>

int main(int argc, char *argv[])
{
    if(argv[4] == "-verbose")
    {
        printf("Verbose is on");
    } 

  return 0;
}

I can use argv[4] in printf() just fine. But i can't compare the content of argv[] to anything.

Why ?

Kind regards Mads Nielsen

toredo 07-24-2011 06:46 PM

You have to use strcmp to compare two c-strings.

With '==' you compare in your case only the strings addresses, which here are never the same.
An you should always check the parameters-count (argc).

best regards
toredo

madsovenielsen 07-25-2011 08:17 AM

Thank you very much!


All times are GMT -5. The time now is 02:46 PM.