Kris H.
Kris H.

Reputation: 107

Quicksorting an array of pointers to structs

I'm currently attempting to use the built-in quicksort provided by C in order to sort an array of pointers to structs. I want to sort each element based on a name element within the struct.

Although my debug output of the entire array each time through the comparison function shows me that the function is indeed shifting elements, the end result is not the correct sorted order. Is there something I'm just not seeing here?

typedef struct // The custom data type.
{
    char *name;
} Person;

----------------------------

Person **people; // A dynamically allocated array of Person pointers.
int numPeople;   // The logical index of people.
int maxPeople;   // The current maximum capacity of people.

int compare(const void *a, const void *b) // The comparison function for determining
{                                         //    alphabetic ordering.
   const Person *const *p1 = a;
   const Person *const *p2 = b;
   return strcmp((*p1)->name, (*p2)->name); // Compare alphabetically, return result.
}

void SomeFunction(void)
{
    qsort(people, numPeople, sizeof(Person *), compare); // Perform the sort.
}

Thanks for help with this.

Upvotes: 1

Views: 323

Answers (2)

shinkou
shinkou

Reputation: 5154

I have tested your code and it looks working OK. Here is the code I compiled with gcc 4.5.2:

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

typedef struct // The custom data type.
{
        char *name;
} Person;

Person **people; // A dynamically allocated array of Person pointers.
int numPeople;   // The logical index of people.
int maxPeople;   // The current maximum capacity of people.

int compare(const void *a, const void *b) // The comparison function for determining
{                                         //    alphabetic ordering.
        const Person *const *p1 = a;
        const Person *const *p2 = b;
        return strcmp((*p1)->name, (*p2)->name); // Compare alphabetically, return result.
}

void SomeFunction(void)
{
        qsort(people, numPeople, sizeof(Person *), compare); // Perform the sort.
}

int main()
{
        int iCnt;

        maxPeople = 4;
        numPeople = 4;

        people = calloc(1, sizeof(Person *) * maxPeople);

        people[0] = calloc(1, sizeof(Person));
        people[1] = calloc(1, sizeof(Person));
        people[2] = calloc(1, sizeof(Person));
        people[3] = calloc(1, sizeof(Person));

        people[0]->name = strdup("Tanya");
        people[1]->name = strdup("Alfred");
        people[2]->name = strdup("Harry");
        people[3]->name = strdup("Oakley");

        for(iCnt = 0; iCnt < numPeople; iCnt ++)
                printf("[%d] %s\n", iCnt, people[iCnt]->name);

        SomeFunction();

        for(iCnt = 0; iCnt < numPeople; iCnt ++)
                printf("[%d] %s\n", iCnt, people[iCnt]->name);

        return 0;
}

The code looks legit and I'm not sure what's wrong. Could you try compiling the code I tested and see if it works?

Upvotes: 1

Anon
Anon

Reputation: 2656

Can you try with this

int compare(const void *a, const void *b) // The comparison function for determining
{                                         //    alphabetic ordering.
   const Person *p1 = *(const Person**)a;
   const Person *p2 = *(const Person**)b;
   return strcmp((p1)->name, (p2)->name); // Compare alphabetically, return result.
}

Upvotes: 0

Related Questions