Thread: segmentation fault

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    segmentation fault

    Does anyone know why i get a segmentation fault when i call scanf() in guessSquare
    function im not sure whether im calling it correctly?


    Code:
    int main(void)
    {
       /* Stores all hidden data about the minefield. */
       char minefield[MAX_GRID][MAX_GRID];
       /* A version of the minefield that only stores known information. */
       char displayGrid[MAX_GRID][MAX_GRID];
       int display;
       char *rSquare;
       unsigned *cSquare;
        /* Number of cells in each row and column used. */
       unsigned size = 0; 
       char inputType;
       /* call functions according to the flow chart diagram*/
       init(minefield, displayGrid);
       size = getSize();
       placeMines(minefield,size);
       displayMinefield(displayGrid,size);
       inputType = guessType();
       guessSquare(rSquare, cSquare, size);
       
       return EXIT_SUCCESS;
    
    /*************************************************/
       void guessSquare(char* row, unsigned* col, unsigned size)
    {
        int valid =0;
        char *r = row;
        unsigned *c = col;
      
       do
       {
             valid = 0;
      
             fscanf(stdin, "%c, %u", r, c);
    	 
             if(*row == 'a')
             {
               valid = 1;
             }
             else if(*row == 'b')
             {
               valid = 1;
             }
             else if(*row == 'c')
             {
               valid = 1;
             }
             else if (*row == 'd')
             {
               valid = 1;
       
             }
             else if(*row == 'e')
             {
               valid = 1;
             }
             else if(*row =='f')
             {
               valid = 1;
             }
    	 else
    	 {
    	     printf("Incorrect row A - F !\n");
    	     readRestOfLine();
    	 }
       }while(!valid);
       
    }
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You forgot to make your pointers point to anywhere.
    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.

  3. #3
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    where abouts in my main where im passing in the values to the parameters for the function?

  4. #4
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    Code:
    char *r = row;
    unsigned *c = col;
    fscanf(stdin, "%c, %u", r, c);
    It assigns address of row to r & col to c,if u still want to make scanf work then ommit those two assignments.Try using
    Code:
    fscanf(stdin, "%c, %u", row, col);
    instead.

    Thanks & regards
    Vinit

  5. #5
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    i still get a seg fault in my code

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > It assigns address of row to r & col to c,if u still want to make scanf work then ommit those two assignments
    Simply copying a pointer to an undefined location doesn't make it a defined pointer.

    In main, you need
    char rSquare;
    unsigned cSquare;
    guessSquare(&rSquare, &cSquare, size);
    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.

  7. #7
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    i got rid of the seg fault now for some reason fscanf isnt taking the value properly because it wont evaluate the if statements properly it keeps printing error even though i type in the correct input


    Code:
    void guessSquare(char* row, unsigned* col, unsigned size)
    {
       int valid =0;
       char r;
       unsigned c;
       
       r = *row;
       c = *col;
       
       printf("Enter a square from row A -F and Col 1-6\n") ;
       do
       {
             valid = 0;
      
             fscanf(stdin, "%c, %u", &r, &c);
    	  
    	  
             if(r == 'a')
             {
               valid = 1;
             }
             else if(r == 'b')
             {
               valid = 1;
             }
             else if(r == 'c')
             {
               valid = 1;
             }
             else if (r == 'd')
             {
               valid = 1;
       
             }
             else if(r == 'e')
             {
               valid = 1;
             }
             else if(r =='f')
             {
               valid = 1;
             }
    	 else
    	 {
    	     printf("error\n");
    	     break;
    	 }
       }while(!valid);
       
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Don't use scanf / fscanf perhaps?

    I dunno - why don't you check the return result of functions?

    Using anything other than fgets() to read input is basically a waste of time IMO. It simply leads to more problems, and there's the whole "get rid of surplus newlines problem" - ick ick ick....

    Here,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void guessSquare(char *row, unsigned int *col, unsigned size);
    
    int main(void)
    {
      char rSquare;
      unsigned int cSquare;
      unsigned int size = 0;
      guessSquare( &rSquare, &cSquare, size);
      printf( "%c %u\n", rSquare, cSquare );
      return EXIT_SUCCESS;
    }
    
    /*************************************************/
    void guessSquare(char *row, unsigned *col, unsigned size)
    {
      char buff[BUFSIZ];
      int valid = 0;
    
      while ( !valid &&
              fgets( buff, sizeof buff, stdin ) != NULL ) {
        if ( sscanf( buff, "%c, %u", row, col ) == 2 ) {
          if ( *row >= 'a' && *row <= 'f' ) {
            valid = 1;
          }
        }
      }
    }
    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.

  9. #9
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    ok iv tried that but why does it keep asking for input after i press enter

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I've no idea - perhaps you didn't enter the required input - say check the caps lock key.

    Do you have a log of the input and output ?

    Try putting some additional code in - say some printf statements in the else part of each of those if statements.
    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. 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
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM