Thread: seg with args

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    seg with args

    I keep getting a segmentation fault when I do this

    for(x=0;x<argc;x++){
    if(strcmp(argv[x],"-n")==0) {
    strlet=argv[x+1];
    tr=1;
    printf("%s\n",strlet);
    }

    but when I do this,

    for(x=0;x<argc;x++){
    if(strcmp(argv[x],"-n")==0) {
    strlet=argv[x+1];
    tr=1;
    printf("%s\n",argv[x+1]);
    }


    i declare strlet like this

    char strlet;

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    strlet is only good for one charater, and i think your supposed to use the argv[]s as strings. so thats not good. also, argv[0] is the apps name, start x off as being 1.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here some things to look over:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
      char mychar;
      if (argc == 2)
      {
        printf ("argv[1] is %s\n", argv[1]);
        if (strncmp(argv[1], "-n", 2) == 0)
        {
          mychar = argv[1][2];
          if (mychar != '\0')
            printf ("found char as %c\n", mychar);
        }
      }
      return(0);
    }
    
    /*
     * When invoked with ./a.exe -nZ
     argv[1] is -nZ
     found char as Z
     *
     */
    And another sample to help you understand the layout of the command line args as they sit in memory:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
      int i, j;
      for (i = 0; i < argc; i++)
        for (j = 0; argv[i][j]; j++)
          printf ("argv[%d][%d] is %c\n", i, j, argv[i][j]);
      return(0);
    }
    
    /*
     * When invoked with a.exe -nZ
     argv[0][0] is E
     argv[0][1] is :
     argv[0][2] is \
     argv[0][3] is a
     argv[0][4] is .
     argv[0][5] is e
     argv[0][6] is x
     argv[0][7] is e
     argv[1][0] is -
     argv[1][1] is n
     argv[1][2] is Z
     *
     */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    thank you all so very much!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM