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

  • Home
  • SEARCH
  • 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 8780085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:57:17+00:00 2026-06-13T19:57:17+00:00

I’m attempting to sort a struct of students in C. I’m using qsort and

  • 0

I’m attempting to sort a struct of students in C. I’m using qsort and doing a simple string comparison. Not sure why I’m getting a seg fault. I am casting and dereferencing the struct appropriately in the comparator function. I’ve been banging my head on this one for a while.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef struct Student {
    char * last;
    char * first;
    char * social;
    int age;
    float gpa;
    enum { ComputerScience = 1, History, Biology } major;
} Student;

void gpa_comp(Student **);
int gpa_comparator(const void * a, const void * b);
void name_comp(Student **);
int name_comparator(const void * a, const void * b);
void make_student_list(Student **);
int student_cmp(const void * a, const void * b);
void print_students (Student ** s, size_t len);
int flt_cmp(const float a, const float b);

int main(int argc, char const *argv[])
{
    int i;
    struct Student ** students = (struct Student **)malloc(5 * sizeof(struct Student *));

    for (i = 0; i < 5; ++i) {
        students[i] = (struct Student *)malloc(sizeof(struct Student));
    }

    make_student_list(students);

    printf("Students in no order\n");
    print_students(students, 5);

    printf("\n");
    printf("Students Sorted By First Name Asc\n");
    name_comp(students);

    return 0;
}

int gpa_comparator(const void * a, const void * b){
    const Student * s1 = (const Student *)a;
    const Student * s2 = (const Student *)b;
    return (int)(100.f*s1->gpa - 100.f*s2->gpa);
}

int name_comparator(const void * a, const void * b){
    const Student * s1 = (const Student *) a;
    const Student * s2 = (const Student *) b;
    return strcmp(s1->first, s2->first);
}

void print_students (Student ** s, size_t len) {
    int c;
    for (c = 0; c < len; c++){
        printf("%s - %s - %2f\n", s[c]->first, s[c]->last, s[c] -> gpa);
    }
}

void gpa_comp(Student ** students) {
    size_t len = 5;
    qsort(students, len, len, gpa_comparator);
    print_students(students, len);
}

void name_comp(Student ** students){
    qsort(students, 5, sizeof(Student), name_comparator);
    print_students(students, 5);
}

void make_student_list(Student ** s){
    s[0]->first = "Dave";
    s[0]->last = "Johnson";
    s[0]->social = "362-89-3284";
    s[0]->major = ComputerScience;
    s[0]->gpa = 3.5;

    s[1]->first = "Sally";
    s[1]->last = "Susie";
    s[1]->social = "251-43-9220";
    s[1]->major = History;
    s[1]->gpa = 3.4;

    s[2]->first = "Molly";
    s[2]->last = "Thomas";
    s[2]->social = "123-45-2343";
    s[2]->major = Biology;
    s[2]->gpa = 3.3;

    s[3]->first = "Brett";
    s[3]->last = "Bigler";
    s[3]->social = "523-364-3462";
    s[3]->major = ComputerScience;
    s[3]->gpa = 3.9;

    s[4]->first = "Sandra";
    s[4]->last = "Salami";
    s[4]->social = "724-457-2455";
    s[4]->major = Biology;
    s[4]->gpa = 4.0;
}
  • 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-06-13T19:57:18+00:00Added an answer on June 13, 2026 at 7:57 pm

    You’re sorting an array of Student*, not an array of Student. So, the call to qsort should pass sizeof(Student*) for the size parameter instead of sizeof(Student), and the comparator functions need to convert the const void * parameters to Student **, and dereference those twice.

    The better solution would probably be to change your array to be just Student* instead of Student**. Then, you don’t need to malloc each individual Student instance, and less code will need to change.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm making a simple page using Google Maps API 3. My first. One marker
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the

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.