1

It's supposed to sort students in alphabetical order by their last names. I tried to do it with qsort, but it doesn't work.

struct student {
    char name[30];
    char lastName[30];
    char grades[30][5];
    int numberOfGrades;
};

int compare(const void *a, const void *b) {
    struct group *s1 = (struct group *)a;
    struct group *s2 = (struct group *)b;
    
    return strcmp(s1->lastName, s2->lastName);
}

int main()
{
    struct student group[30];
    group[0].lastName = "Malaska";
    group[1].lastName = "Kowalski";
    group[2].lastName = "Adamczyk";
    group[3].lastName = "Bakiewicz";
    
    int numberOfStudents = 4;

    qsort(group, numberOfStudents, sizeof(group), compare);
}
4
  • stackoverflow.com/q/6105513/14170686 Commented Dec 30, 2020 at 19:36
  • For one thing, you can't assign a string value with group[0].lastName = "Malaska"; please use strcpy(group[0].lastName, "Malaska"); Another, the compare() function is casting to a different type that you have not shown. Commented Dec 30, 2020 at 19:41
  • You should pass sizeof(struct student) instead of sizeof(group) Commented Dec 30, 2020 at 19:41
  • The sizeof(group) should be sizeof(group[0]) or sizeof(struct student) Commented Dec 30, 2020 at 19:43

1 Answer 1

1

The following is an adaptation of your code with corrections and comments:

struct student {
    char name[30];
    char lastName[30];
    char grades[30][5];
    int numberOfGrades;
};

//Note, error incomplete type occurs 
//when using `struct group *s1 = (struct group *)a;`
//will replace with `struct student` 

int compare(const void *a, const void *b) {
    const struct student *s1 = (struct student *)a;
    const struct student *s2 = (struct student *)b;
    
    return strcmp(s1->lastName, s2->lastName);
}

int main()
{
    struct student group[30];//local scope, will not be recognized in 'compare()'                      
    strcpy(group[0].lastName, "Malaska");//string assignments need to use strcpy or similar, not =
    strcpy(group[1].lastName, "Kowalski");
    strcpy(group[2].lastName, "Adamczyk");
    strcpy(group[3].lastName, "Bakiewicz");
    
    int numberOfStudents = 4;

    //sending correct size of each element 
    qsort(group, numberOfStudents, sizeof(group[0]), compare);
    //                             ^^^^^^^^^^^^^^^^^
}

The following is also an adaptation of your code, but with some additional changes to construct that if in scope of what you are learning, should provide a few more examples of readability, use of scope, use of typedef, and dynamic memory allocation and freeing...

typedef struct {  //use of `typedef struct {` rather than 'struct name {` 
    char name[30];
    char lastName[30];
    char grades[30][5];
    int numberOfGrades;
}student_s; //new type, can be used in place of `struct student` anywhere.

int compare(const void *a, const void *b) {
    const student_s *s1 = (student_s *)a;
    const student_s *s2 = (student_s *)b;
    
    return strcmp(s1->lastName, s2->lastName);
}

int main()
{
    int numberOfStudents = 4;//moved to top so can use group
                             //to properly size instances of group
    //dynamic memory allocation, and clean up at bottom
    student_s *group = calloc(numberOfStudents, sizeof *group);
    if(group)//test here to ensure success
    {
        strcpy(group[0].lastName, "Malaska");
        strcpy(group[1].lastName, "Kowalski");
        strcpy(group[2].lastName, "Adamczyk");
        strcpy(group[3].lastName, "Bakiewicz");

        qsort(group, numberOfStudents, sizeof(group[0]), compare);
        //... more code as necessary
        //when finished with 'group', free it.
        free(group);
    }
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.