Thread: segmentation fault while trying to search

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    46

    segmentation fault while trying to search

    hello i am getting a segmentation fault right after i ask for the social security number in the print statement. i am not sure if the fault is in the search or recsearch function. please someone help

    Code:
    int srch (STUDENT student[], char* ssn)
    {
      
      char* Name = "";
      int tempstu = -1;  
    
      while (strcmp(ssn, "q"))
       {
         printf("\n Please enter social security number (press q to quit): ");    
         scanf("%s", ssn);
         tempstu = recSrch(student, ssn, 0, SIZE - 1);
         printf("\nRecord found at index %d:", tempstu);
         strcat(strcat(strcpy(Name, student[tempstu].fName), " "), 
    student[tempstu].lName);
         printf("\n%30s %9d  %13s     %02d/%02d/%4d", Name,
                 student[tempstu].grade, student[tempstu].ssn,
                 student[tempstu].bDate.month, student[tempstu].bDate.date,
                 student[tempstu].bDate.year);
    }}
    /*===============================recSrch=====================================*/
    // this function searches for the input social security number and returns
    // the result
    
    int recSrch (STUDENT student[], char* ssn, int lb, int ub)
    {
       int result;
       int mid = (lb + ub) / 2;
    
       if (lb > ub)
          {      
           result = -1;
          }
       else if (strcmp(ssn, student[mid].ssn) == 0)
               {
                result = mid;
               }    
       else if (strcmp(ssn, student[mid].ssn) < 0)
               {      
                result = recSrch(student, ssn, lb, mid - 1);
               }
       else
        { 
         result = recSrch(student, ssn, mid + 1, ub);
        }
       return result;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    oh and i am not even getting the print statement..."Record found at &#37;d index"

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Show more of your code. I think I know what the issue is but I need your code that calls the function to be certain.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    Code:
    #define MAX 20
    #define SIZE 10
    
    // creates a nested structure for student names, birthdates, and social
    // security numbers
    typedef struct
    {
       int month;
       int date;
       int year;
    } DATE;
    
    typedef struct
    {
       char fName[MAX];
       char lName[MAX];
       int grade;
       char ssn[11];
       DATE bDate;
    } STUDENT;
    
    /* prototype declarations */
    bool openFile (FILE **fpinfile_txt, char* infile);
    int convertMonth(char * monthStr);
    int fillArr (FILE *fpinfile_txt, STUDENT* myStudents, int numRecords);
    void sortArr(STUDENT student[], int last);
    void exchangeSmallest (STUDENT student[], int i, int last);
    void prtArr(STUDENT* student, int numRecords);
    int srch (STUDENT student[], char* ssn);
    int recSrch (STUDENT student[], char* ssn, int lb, int ub);
    
    int main(int argc, char* argv[])
    {
       STUDENT myStudents[SIZE];
       char *infile;
       int numRecords;
       int count;
      
    
       if (argc != 3)
       {
        printf("\nFile not found\n");
       }
        else 
          {
           count = atoi(argv[1]);
           infile = argv[2];
             if (openFile (&fpinfile_txt, infile))
             {
               if(fillArr(fpinfile_txt, myStudents, count) != 1)
               {             
                   prtArr(myStudents, count);
                   sortArr(myStudents, count);
                   prtArr(myStudents, count);
                   srch(myStudents, ssn);
               }
             }
          }
       return 0;
    }

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by starvinmarvin View Post
    Code:
    #define MAX 20
    #define SIZE 10
     
    // creates a nested structure for student names, birthdates, and social
    // security numbers
    typedef struct
    {
       int month;
       int date;
       int year;
    } DATE;
     
    typedef struct
    {
       char fName[MAX];
       char lName[MAX];
       int grade;
       char ssn[11];
       DATE bDate;
    } STUDENT;
     
    /* prototype declarations */
    bool openFile (FILE **fpinfile_txt, char* infile);
    int convertMonth(char * monthStr);
    int fillArr (FILE *fpinfile_txt, STUDENT* myStudents, int numRecords);
    void sortArr(STUDENT student[], int last);
    void exchangeSmallest (STUDENT student[], int i, int last);
    void prtArr(STUDENT* student, int numRecords);
    int srch (STUDENT student[], char* ssn);
    int recSrch (STUDENT student[], char* ssn, int lb, int ub);
     
    int main(int argc, char* argv[])
    {
       STUDENT myStudents[SIZE];
       char *infile;
       int numRecords;
       int count;
     
     
       if (argc != 3)
       {
        printf("\nFile not found\n");
       }
        else 
          {
           count = atoi(argv[1]);
           infile = argv[2];
             if (openFile (&fpinfile_txt, infile))
             {
               if(fillArr(fpinfile_txt, myStudents, count) != 1)
               {             
                   prtArr(myStudents, count);
                   sortArr(myStudents, count);
                   prtArr(myStudents, count);
                   srch(myStudents, ssn);
               }
             }
          }
       return 0;
    }
    What the... Where is this declared?

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    33
    lol, I was thinking about posting this, but you beat me to it, starvin. Basically what's wrong is it's supposed to return the correct social security number when input along with the name and index, quit the function if you type in q, or say that no record was found if you put in a number that's not in the search. Do you guys think the problem is in any of these functions or could they be in another function?

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    Code:
    int fillArr (FILE *fpinfile_txt, STUDENT* myStudents, int numRecords) 
    {
    
       int i;
       int scanRes;
       int stop = 0;
       int error = 0;
       char fName[MAX];
       char lName[MAX];
       int grade;
       char ssn[MAX];
       char month[MAX];
       int date;
       int year;

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok. As I kind of suspected....

    Code:
    int srch (STUDENT student[], char* ssn)
    { 
      char* Name = "";//NO!
      int tempstu = -1;  
    
      while (strcmp(ssn, "q"))//NO!
    
      {
         printf("\n Please enter social security number (press q to quit): ");    
         scanf("&#37;s", ssn);
         tempstu = recSrch(student, ssn, 0, SIZE - 1);
         printf("\nRecord found at index %d:", tempstu);
         strcat(strcat(strcpy(Name, student[tempstu].fName), " "), student[tempstu].lName);
         printf("\n%30s %9d  %13s     %02d/%02d/%4d", Name,
                 student[tempstu].grade, student[tempstu].ssn,
                 student[tempstu].bDate.month, student[tempstu].bDate.date,
                 student[tempstu].bDate.year);
      }
    }
    What you are wanting to do is something like this:
    Code:
    int srch (STUDENT student[], char* ssn)
    {
      char Name[256] = "";
      int tempstu = -1;  
    
      ssn[0] = '\0';
    
      while (strcmp(ssn, "q"))
      {
         printf("\n Please enter social security number (press q to quit): ");    
         scanf("%9s", ssn);
         tempstu = recSrch(student, ssn, 0, SIZE - 1);
         printf("\nRecord found at index %d:", tempstu);
         strcat(strcat(strcpy(Name, student[tempstu].fName), " "),  student[tempstu].lName);
         printf("\n%30s %9d  %13s     %02d/%02d/%4d", Name,
                 student[tempstu].grade, student[tempstu].ssn,
                 student[tempstu].bDate.month, student[tempstu].bDate.date,
                 student[tempstu].bDate.year);
      }
    }
    Last edited by master5001; 12-02-2008 at 07:18 PM.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    wait i forgot i changed it after that
    here is where it is declared
    Code:
    int main(int argc, char* argv[])
    {
       char* ssn = " ";
       STUDENT myStudents[SIZE];
       char *infile;
       int numRecords;
       int count;
       FILE *fpinfile_txt;

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok. That is even more like what I suspected you had done I was worried that I totally lost my mind!

    char ssn[MAX] = ""; is how it should be declared.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    ok i tried that but now i get a invalid initliazer for char* [256] = "";

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then you didn't "try that" -- if you had, there wouldn't be an * in your error message.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Perhaps I should make my entire posts bolded sometime just to see what happens...

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Since "why" is always more useful than shouting "NO!" at someone, though infinitely less fun. Here is the deal, when you say char *x = "y" you are pointing x to a string stored somewhere in your program. When you say char x[20] = "" you are saying I have an array of 20 chars named x, and I wish to fill it with "". Make sense? So when you start trying to write to that place where there is a string "y" you are going to also be buffer overflowing into god only knows what.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    thanks it got rid of the segmentation fault however now it says
    something like

    Record found at index -1
    with some weird charcters and 42 P 00/-173943938493/27

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM