Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7522985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:40:00+00:00 2026-05-30T02:40:00+00:00

The program is reading line by line from a file and storing info in

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:40:02+00:00Added an answer on May 30, 2026 at 2:40 am

    Provided STUDENT_DATA >= 8, there’s only one possible explanation, which is that one or more of your lastName fields was never initialized and contains NULL or garbage. If your loop initializing these fields contained the same error as your loop printing it out (using students=students+sizeof(StudentInformation) instead of students++), that would be why.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C++ program that is reading in info from a file, that
I have a Perl program that is reading html tags from a text file.
I have some trouble reading from an ini file using boost program options. The
I'm reading lines from a text file using (line-seq (reader input.txt)) . This collection
I have a program that reads from a txt file line by line splits
I am reading a binary file into a parsing program. I will need to
I wrote a program which includes writing and reading from database. When I run
I am reading a file line by line and adding each line to a
I am writing an application in C# that reads info from a .mdb file
I have a program that load data from a file using std::ifstream and store

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.