The program is reading line by line from a file and storing info in a struct. Everything works except for sorting the array of structs. For example, at the end when I’m printing the struct(code included at the end), it works completely fine.
The problem (segmentation fault) occurs when I call the qsort.
Also, printing students[0].lastName works fine, but printing students[1].lastName returns a (null), that too is confusing.
I’ve looked everywhere and my code seems very similar to what has been posted as correct solutions to sorting structs, so I’m very confused.
Defining struct in header of main:
// DEFINE STRUCT
typedef struct _StudentInformation {
int term;
int studentId;
char *lastName;
char *firstName;
char *subject;
int catalogNumber;
char *section;
} StudentInformation;
Allocating the struct in main method (STUDENT_DATA=50):
// ALLOCATE AN ARRAY OF STUDENTS (STRUCT)
StudentInformation *students;
if ((students = malloc(STUDENT_DATA*sizeof(StudentInformation)))==NULL) {
scanf("Error can't allocate enough students!\n");
exit(1);
}
The problem: Calling quicksort (the reason for the 8 is because there are 8 entries THAT WORK and are LOADED, even less than 8 doesn’t work).:
qsort(students, 8, sizeof(StudentInformation), comparator);
Comparator for quicksort:
int comparator (const void * a, const void * b) {
StudentInformation *s1 = (StudentInformation*)a;
StudentInformation *s2 = (StudentInformation*)b;
return strcmp(s1->lastName, s2->lastName);
}
The reason I know data is loaded fine is because printing works completely fine:
void printInformation (StudentInformation *students) {
// PRINT EVERYTHING
while(students->firstName!=NULL) {
printf("%-s, %s %15d %4d %4s%d %7s\n",
students->lastName,students->firstName,students->term,
students->studentId, students->subject,students->catalogNumber,
students->section);
// Increment
students=students+sizeof(StudentInformation);
}
}
What it prints (i only included 2 out of the 8 that were printed, no NULLS printed):
Castille, Michael Jr 1201 103993269 CSE230 R03
Boatswain, Michael R. 1201 105515018 CSE230 R01
Thank you!
Provided STUDENT_DATA >= 8, there’s only one possible explanation, which is that one or more of your
lastNamefields was never initialized and contains NULL or garbage. If your loop initializing these fields contained the same error as your loop printing it out (usingstudents=students+sizeof(StudentInformation)instead ofstudents++), that would be why.