Thread: getopt()

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    getopt()

    i have problems using the getopt().
    Code:
    ...
    
    char lala, test[2];
    
    while((lala = getopt(argc, argv, "m:S:")) != EOF)
    {
       switch(lala)
       {
          case 'm':
          {
              printf("%s\n", optarg);
              ....
              break;
          }
    
          case 'S':
          {
              printf("%s\n", optarg);
              sscanf(optarg, "%x:%x", test[0], test[1]);
              break;
          }
          
         default: break;
        }
    }
    when i use printf() to print the optarg of case S, it tells me that it is (null).
    so i get a segmentation violation.
    why is optarg (null)?

    thanks

  2. #2
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Originally posted by vVv
    getopt( ) returns int, not char.
    so what i am supposed to change?

    lets assume i run the code like that:

    Code:
    ./program -m printit -S ff:ff
    output:
    
    printit
    SIGSEGV
    as it seems there is no problem with printing the string "printit" for case 'm'.
    but why is optarg = null in case S only?

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getopt question
    By -EquinoX- in forum C Programming
    Replies: 5
    Last Post: 03-26-2009, 08:42 PM
  2. Case-insensitive getopt()
    By Ehtyar in forum C Programming
    Replies: 10
    Last Post: 11-20-2008, 03:21 AM
  3. getopt help
    By wuzzo87 in forum C Programming
    Replies: 12
    Last Post: 04-09-2007, 03:16 AM
  4. getopt and multiple options
    By bej in forum Linux Programming
    Replies: 2
    Last Post: 11-17-2005, 04:51 PM
  5. getopt() and weird output
    By kristy in forum C Programming
    Replies: 2
    Last Post: 06-28-2003, 04:39 PM