3

我正在尝试对 C 中的学生结构进行排序。我正在使用 qsort 并进行简单的字符串比较。不知道为什么我会遇到段错误。我在比较器函数中适当地转换和取消引用该结构。一段时间以来,我一直在努力解决这个问题。

#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;
}
4

1 回答 1

9

你正在排序一个数组Student*,而不是一个数组Student。因此,对的调用qsort应该传递参数而不是,并且比较器函数需要将参数转换为,并取消引用两次。sizeof(Student*)sizesizeof(Student)const void *Student **

更好的解决方案可能是将您的数组更改为 justStudent*而不是Student**. 然后,您不需要malloc每个单独的Student实例,并且需要更改的代码更少。

于 2012-11-05T19:49:29.407 回答