using qsort() to sort pointers to structs containing strings

前端 未结 3 1271
Happy的楠姐
Happy的楠姐 2021-01-13 02:58

I\'m not sure if this is possible to do with qsort because what I want to sort (array of pointers to struct) is not what I am comparing (strings).

Here is an abridge

3条回答
  •  不思量自难忘°
    2021-01-13 03:30

    What will be passed to cmp() are struct student** parameters (in the guise of void*). So change cmp() like so:

    int
    cmp(const void *p0, const void *p1)
    {
            struct student* ps0 = *(struct student**) p0;
            struct student* ps1 = *(struct student**) p1;
    
            return strcmp( ps0->lname, ps1->lname);
    }
    

提交回复
热议问题