Thread: get cmd arguments - what am i do ing wrong here

  1. #1
    Registered User
    Join Date
    Jan 2011

    get cmd arguments - what am i do ing wrong here

    Hi i cannot find u bug but i get segmentation fault

    #include <stdio.h>     /* for printf */
    #include <stdlib.h>    /* for exit */
    #include <unistd.h>    /* for getopt */
    /* define argument container */
    typedef struct args{
      char *i;          /* name of first input file */
      char *o;          /* name of output file */
      int b;            
      int l;            
     } Args;
    Args *getArgs(int argc, char **argv){
      char ch;                   /* service variables */
      int i;
      Args *args;
      args = (Args *)malloc(sizeof(Args));
      args->i = NULL;
      args->o = NULL;
      args->b = 0;
      args->l = 0;
      while ((ch = getopt(argc, argv, "ioblh:")) != -1) {     
            switch (ch) {       
                case 'i':       
                    args->i = optarg;  
                    printf("%s\n",optarg);   // HERE IS THE PROBLEM!
                case 'o':       
                    args->o =optarg;
                case 'b':
                    args->b = atoi(optarg);
                case 'l':
                case '?':
                 if (optopt == 'i' || optopt == 'o' || optopt == 'b' )
                   fprintf (stderr, "Option -%c requires an argument.\n", optopt);
                 else if (isprint (optopt))
                   fprintf (stderr, "Unknown option `-%c'.\n", optopt);
                   fprintf (stderr,
                            "Unknown option character `\\x%x'.\n",
                case 'h':  
                    printf("Usage:\n%s [options]\n", argv[0]);
                    printf("\t-i\tInput file : def <stdin>\n");
                    printf("\t-o\tOutput file : def <stdout>\n");
                    printf("\t-b\tReport binary map for -b : require INT\n");
                    printf("\t-l\tReport log10 values : no input\n");
                    printf("\t-h\tPrint this\n");
      return args;
    int main(int argc, char *argv[]){
      Args *args;
       args = getArgs(argc, argv);
        printf("You requested:\n");
        if (args->i != NULL) printf("i [%s]\n", args->i);
        if (args->o != NULL) printf("o [%s]\n", args->o);
        if (args->b > 0) printf("b [%d]\n", args->b);
        if (args->l > 0) printf("l [%d]\n", args->l); 
    Seg. F. is in line 32 !

    thank you
    Last edited by baxy; 11-21-2012 at 06:02 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Learning to debug.
    $ gcc -g foo.c
    $ gdb ./a.out
    GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
    Copyright (C) 2011 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu".
    For bug reporting instructions, please see:
    Reading symbols from /home/sc/Documents/a.out...done.
    (gdb) run -i ggggggg
    Starting program: /home/sc/Documents/a.out -i ggggggg
    Program received signal SIGSEGV, Segmentation fault.
    __strlen_sse2 () at ../sysdeps/x86_64/strlen.S:32
    32	../sysdeps/x86_64/strlen.S: No such file or directory.
    	in ../sysdeps/x86_64/strlen.S
    (gdb) bt
    #0  __strlen_sse2 () at ../sysdeps/x86_64/strlen.S:32
    #1  0x00007ffff7aa5f6c in _IO_puts (str=0x0) at ioputs.c:37
    #2  0x0000000000400836 in getArgs (argc=3, argv=0x7fffffffe268) at foo.c:32
    #3  0x00000000004009bc in main (argc=3, argv=0x7fffffffe268) at foo.c:70
    (gdb) frame 2
    #2  0x0000000000400836 in getArgs (argc=3, argv=0x7fffffffe268) at foo.c:32
    32	                printf("%s\n",optarg);   // HERE IS THE PROBLEM!
    (gdb) print optarg
    $1 = 0x0
    Yes, printing a NULL pointer is indeed a problem.

    Quote Originally Posted by man page
    The options argument is a string that specifies the option characters that are valid for this program. An option character in this string can be followed by a colon (‘:’) to indicate that it takes a required argument.
    "ioblh:" doesn't mean they all have optional arguments.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-14-2011, 06:35 PM
  2. Can't figure out what's wrong with Function's Arguments
    By jamez05 in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2006, 10:30 AM
  3. passing arguments using "Command Line Arguments"
    By Madshan in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2006, 03:46 PM
  4. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  5. Replies: 9
    Last Post: 07-15-2004, 03:30 PM