Command Line Argument Comparison

This is a discussion on Command Line Argument Comparison within the C++ Programming forums, part of the General Programming Boards category; Hmm, I'm having some trouble with this. I want to check if the second argument (argv[1]) is equal to "-debug". ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    37

    Command Line Argument Comparison

    Hmm, I'm having some trouble with this.

    I want to check if the second argument (argv[1]) is equal to "-debug".

    so what I've done is...well, see below. None of the if statements register as true.

    Code:
    char *debugArg = argv[1];
    
    if (debugArg == "-debug")
    if (debugArg == "-debug\0")
    if (debugArg == "-debug\n")
    None of them work! The middle one with the \0 should...I tested it manually to make sure that letter was a \0, and it is. What's wrong with this?

    The only solution I have right now is, unfortunately,:

    Code:
    if (debugArg[0] == '-' &&
      debugArg[1] == 'd' &&
      debugArg[2] == 'e' &&
      debugArg[3] == 'b' &&
      debugArg[4] == 'u' &&
      debugArg[5] == 'g')
    ...but that kinf of sucks. What's the solution to this? Maybe I could use strings somehow...

    --Ashiq

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571

    char *debugArg = argv[1];

    if (debugArg == "-debug")
    if (debugArg == "-debug\0")
    if (debugArg == "-debug\n")
    The problem here is that you're comparing POINTERS, not strings. if (debugArg == "-debug") is testing if the memory location where the first string resides is the same as the memory location in which a string literal resides, and it can never happen.

    2 options:

    1) The C-way - use strcmp().
    2) The C++ way - do the following:

    #include <string>

    std::string debugArg = argv[1];

    if (debugArg == "-debug"){
    /*...*/
    }

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    if(!strcmp(debugArg, "-debug")) ... or equivalently ...
    if(strcmp(debugArg, "-debug") == 0)
    is the C way of doing it.

    You also might want strcpy() instead of assignment (debugArg = argv[1]) for the same reason Cat mentioned. It will work that way (as they point to the same place), but when not intentional, its good not to have two values pointing at the same place (in the event that one gets modified, the other would also be modified as it points to the same place).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>in the event that one gets modified
    Normally, you wouldn't want to actually change the command line args, mostly you just read them. There's no need to make a copy if you're only reading them... (personal preference of course )
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> Normally, you wouldn't want to actually change the command line args, mostly you just read them.

    Sure there is! In case you want to change the args to what the user "meant" to put in.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Sure there is! In case you want to change the args to what the user "meant" to put in.
    In which case you just ignore them and go off doing whatever you want, just like any other Windows program
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    37
    Ack...sorry. Hammer: the FAQ is for character array manipulation...which is cool...but strings would've been better

    Anways...I realized the solution, and yes, I remember that it's pointers I'm comparing...damn stupid thing to do...but here's what I did anyway.

    Code:
    char *debugArg = argv[1];
    string debugStr = debugArg;
    
    if (debugStr.compare("-debug\0") == 0)
    { ... }
    ...which worked. Thanks for all the help!

    --Ashiq

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You don't need the explicit \0 in there... "-debug" will work just fine. Also, since you are using string, operator== will work just fine (since you are no longer comparing pointers).

    >> In which case you just ignore them and go off doing whatever you want, just like any other Windows program

    Alright, just need to change the parameters explicitly for Linux programs then.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. The biggest project I've worked on: ASCIIpOrtal
    By guesst in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 07-21-2009, 04:42 AM
  3. The reversed-comparison trick: an argument against
    By brewbuck in forum Tech Board
    Replies: 8
    Last Post: 02-24-2009, 08:25 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 08:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21