Thread: Help with code segmentation fault

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    40

    Help with code segmentation fault

    ok heres the code i have now i keep getting a "Segmentation Fault (core dumped)" error can anyone see why? i think it has to do with the loop that i have highlighted in red.

    Code:
    #include <stdio.h>
    
    main()
    {
         char board[30][30];
         int b_rows, b_cols;
         char gem[27], temp[27];
         int gem_value[27];
         char c, gem_letter;
         int rows, cols, j, i, first, column;
    
         scanf("%d%d%\n", &b_rows, &b_cols);
    
    for (i = 0; (first = getchar()) != EOF; i++)
      {
            if (first == 'e')
              {
                first = getchar();
                if(first == 'n')
                  {
                      first = getchar();
                      if(first == 'd')
                        {
                            break;
                        }
                  }
              }
    
            gem[i] = (char)first;
            if (scanf("%*[^0123456789]%d%*[^\n]", &gem_value[i]) != 1)
                break;
    
            if(first !='e')
                getchar();
      }
    
        for (j = 0; j < i; j++)
            printf("%c -- %d\n", gem[j], gem_value[j]);
    
         for(rows=0; rows<b_rows; rows++)
           for(cols=0; cols<b_cols; cols++)
             board[rows][cols] = '.';
    
    
    while((first = getchar()) != EOF)
      {
           gem_letter = (char)first;
           scanf("%c", &column);
    
           for(rows=b_rows; rows>0; rows++)
             {
                if(board[rows][column] == '.')
                  {
                      board[rows][column] = gem_letter;
                  }
             }
    
      }
     
    
         for(rows=0; rows<b_rows; rows++)
           {
             for(cols=0; cols<b_cols; cols++)
               printf("%c", board[rows][cols]);
             printf("\n");
           }
    
         return 0;
    }

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Put some printf()'s so we know what you are doing and also to use as a debug helper. Put a crap load to figure out where it stops.
    Last edited by stumon; 04-10-2003 at 02:57 PM.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The ascii code for the '1' character (ie %c) is 49.
    Code:
    char c = '1'; //c as a value of 49
    Take a closer look at your for loop in red....."rows++" particularly.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  2. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  3. Searching and matching strings: segmentation fault
    By Smola in forum C Programming
    Replies: 18
    Last Post: 07-11-2005, 12:25 AM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  5. Segmentation Fault printing an array of structs
    By ccoder01 in forum C Programming
    Replies: 1
    Last Post: 04-17-2004, 07:03 AM