Thread: newbie: logical && comparison questions

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    61

    newbie: logical && comparison questions

    Code:
    #include <stdio.h>
    int main(int argc, char* argv[])
    {
    argc--;
    ++argv;
    if(*argv)
    {
    while(argv[0][0] == ‘-‘ && argv[1] && argv[2])
    {
    
    }
    
    return 0;
    }
    lets say we run it by typing ./hello -argument1 -argument2 -argument3. why does the comparison fail at the while conidition??

    argv[1] is -argument2 and argv[2] is -argument2. going by precedence, we comparre argv[0][0] == ‘-‘ && argv[1] first and this fails why?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Do a printf on argv[0] and you'll see why it fails.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Except for the whole "we've incremented argv" thing. Once argv++ has happened, argv[0] is -argument1 and argv[1] is -argument2 ... and argv[2] is NULL.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should check the argc value before accessing any argv[i] except argv[0]
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I missed that. And you missed there are 3 args!

    I ran the program. Works for me.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    hmm i found out where my problem is. the comparison should be true but by copying the arguments from pdf to the command line turns -argument into --argument.

    Anyway, am i right to say that for logical && operater,

    while(argv[0][0] == ‘-‘ && argv[1] && argv[2])

    argv[x] will always be true if argv at index x contains non null value.

    thanks alot for the fast responses.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    argv[x] will always be true if argv at index x contains non null value.
    Where is it written that the argv[x] could contain a NULL value? if x is >= argc - it is just an out-of-bounds access resulting in undefined behavior
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Where is it written that the argv[x] could contain a NULL value? if x is >= argc - it is just an out-of-bounds access resulting in undefined behavior
    Not quite. argv[argc] is a null pointer, according to C99.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory handling functions (newbie questions)
    By PaulBlay in forum C Programming
    Replies: 6
    Last Post: 03-10-2009, 06:37 AM
  2. newbie questions
    By raptorx in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 09:30 PM
  3. Real newbie with VC6 questions
    By MagiZedd in forum Windows Programming
    Replies: 8
    Last Post: 10-15-2001, 08:27 PM
  4. newbie questions again
    By dune911 in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2001, 02:43 PM
  5. AND OR NOT logical && || !
    By sballew in forum C Programming
    Replies: 2
    Last Post: 09-03-2001, 03:06 PM