title: C quick sort of a struct causes segmentation fault
body: <p>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.</p>
  
  <p>The problem (segmentation fault) occurs when I call the qsort.</p>
  
  <p>Also, printing students[0].lastName works fine, but printing students[1].lastName returns a (null), that too is confusing.</p>
  
  <p>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.</p>
  
  <p><strong>Defining struct in header of main:</strong></p>
  
  <pre><code>// DEFINE STRUCT
  typedef struct _StudentInformation  {
      int term;
      int studentId;
      char *lastName;
      char *firstName;
      char *subject;
      int catalogNumber;
      char *section;
  } StudentInformation;
  </code></pre>
  
  <p><strong>Allocating the struct in main method (STUDENT_DATA=50):</strong></p>
  
  <pre><code>// 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);
  }
  </code></pre>
  
  <p><strong>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).:</strong></p>
  
  <pre><code>qsort(students, 8, sizeof(StudentInformation), comparator);
  </code></pre>
  
  <p><strong>Comparator for quicksort:</strong></p>
  
  <pre><code>int comparator (const void * a, const void * b) {
      StudentInformation *s1 = (StudentInformation*)a;
      StudentInformation *s2 = (StudentInformation*)b;
  
      return strcmp(s1-&gt;lastName, s2-&gt;lastName);
  }
  </code></pre>
  
  <p><strong>The reason I know data is loaded fine is because printing works completely fine:</strong></p>
  
  <pre><code>void printInformation (StudentInformation *students) {
      // PRINT EVERYTHING
          while(students-&gt;firstName!=NULL) {
              printf("%-s, %s %15d %4d %4s%d %7s\n",
                      students-&gt;lastName,students-&gt;firstName,students-&gt;term,
                      students-&gt;studentId, students-&gt;subject,students-&gt;catalogNumber,
                      students-&gt;section);
  
              // Increment
              students=students+sizeof(StudentInformation);
          }
  }
  </code></pre>
  
  <p><strong>What it prints (i only included 2 out of the 8 that were printed, no NULLS printed):</strong></p>
  
  <pre><code>Castille, Michael Jr            1201 103993269  CSE230     R03
  Boatswain, Michael R.            1201 105515018  CSE230     R01
  </code></pre>
  
  <p>Thank you!</p>