Like Tree1Likes
  • 1 Post By Salem

get cmd arguments - what am i do ing wrong here

This is a discussion on get cmd arguments - what am i do ing wrong here within the C Programming forums, part of the General Programming Boards category; Hi i cannot find u bug but i get segmentation fault Code: #include <stdio.h> /* for printf */ #include <stdlib.h> ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    176

    get cmd arguments - what am i do ing wrong here

    Hi i cannot find u bug but i get segmentation fault

    Code:
    #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("XX\n");
                    printf("%s\n",optarg);   // HERE IS THE PROBLEM!
                    break;
                case 'o':       
                    args->o =optarg;
                    break;
                case 'b':
                    args->b = atoi(optarg);
                    break;
                case 'l':
                    args->l=1;
                    break;
                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);
                 else
                   fprintf (stderr,
                            "Unknown option character `\\x%x'.\n",
                            optopt);
                   break;
                case 'h':  
                default:
                    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");
                    
                    exit(EXIT_FAILURE);        
          }
      }
      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 05:02 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,552
    Learning to debug.
    Code:
    $ 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 <http://gnu.org/licenses/gpl.html>
    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:
    <http://bugs.launchpad.net/gdb-linaro/>...
    Reading symbols from /home/sc/Documents/a.out...done.
    (gdb) run -i ggggggg
    Starting program: /home/sc/Documents/a.out -i ggggggg
    XX
    
    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.
    rags_to_riches likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-14-2011, 05: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-27-2005, 11:48 PM
  5. Replies: 9
    Last Post: 07-15-2004, 03:30 PM

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