strcmp, any borders?

This is a discussion on strcmp, any borders? within the C Programming forums, part of the General Programming Boards category; Hi again! My lovely Compiler, lcc-win32, do not accept this code! Or lets say it this way: It can be ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    34

    Angry strcmp, any borders?

    Hi again! My lovely Compiler, lcc-win32, do not accept this code! Or lets say it this way: It can be compiled and linked, but if I run the code it crashes! WHY?

    Code:
    int main(int argc, char *argv[]);
    
    if (argc < 2)
    //Usage Menu
    
    else if (strcmp (argv[1], "/usageone") == 0)
    {
    //Usage one
    }
    else if (strcmp (argv[1], "/usagetwo") == 0)
    {
           if (strcmp (argv[2], NULL) == 0)
           {
           printf("No second usage!\n");
           return 0;
           }
           else
           // Usage two
    }
    else
    {
    printf("no usage!\n");
    return 0;
    }
    }
    WHY?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    34

    Talking Thx

    I think I am very stupid! This is another idea, too! OF COURSE! I could eat my ..........! But OK, this is a better way, but way does strcmp crash at this time! "NULL" means nothing, why can't I do this in this way?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Are you talking about passing strcmp a NULL argument? If so then of course the program will break, strcmp is more often than not written to assume that both arguments are valid pointers which can be dereferenced and end with a nul character ('\0'). A NULL pointer cannot be dereferenced so strcmp chokes on it.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This part of your code is also troublesome (the bold part)
    Code:
    int main(int argc, char *argv[]);
    
    
    if (argc < 2)
    //Usage Menu
    
    else if (strcmp (argv[1], "/usageone") == 0)
    {
    //Usage one
    }
    else if (strcmp (argv[1], "/usagetwo") == 0)
    {
           if (strcmp (argv[2], NULL) == 0)
           {
           printf("No second usage!\n");
           return 0;
           }
           else
           // Usage two
    }
    else
    {
    printf("no usage!\n");
    return 0;
    }
    }
    Your comment leads me to believe you omitted code after the if statement for the sake of simplicity. However, if this isn't the case you should be getting an error.

    [edit]
    It just occured to me that I may be dealing with a newbie here so I should probably explain why this could be trouble. After any if statement you need either braces that enclose a set of commands, or a single command following the if. So you would just do
    Code:
    if(whatever)
       ;
    [/edit]
    Last edited by master5001; 11-15-2002 at 02:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 05:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 01:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 12:04 PM

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