Hello,
I've searched through many code examples and think that I have the correct compare function, but my problem is that when I run the code, I get an error EXC_BAD_ACCESS. XCode points to the cmpfunc function as the culprit. I know I have allocated memory to all my elements, as I can print the array before it gets passed to qsort. Could someone point me in the right direction for where I am having issues? Please and thank you very much!
Below are my structs so you can see how many levels down I am when I am qsorting:
Code:
typedef struct {
unsigned int siteId;
unsigned int tableTypeId;
unsigned int surMatId;
unsigned int strucMatId;
char *streetAve;
unsigned int neighbourhoodId;
char *neighbourhoodName;
unsigned int ward;
char *latitude;
char *longitude;
} Entries;
typedef struct {
int size;
Entries **entry;
} PicnicTable;
typedef struct {
Table *tableTypeTable;
Table *surfaceMaterialTable;
Table *structuralMaterialTable;
NeighbourHoodTable *neighborhoodTable;
PicnicTable *picnicTableTable;
} DataBase;
extern DataBase *DB;
Code:
int cmpfunc(const void *a, const void *b) {
return strcmp((*(Entries**)a)->neighbourhoodName, (*(Entries**)b)->neighbourhoodName);
}
// just ignore the below, I was experimenting
// Entries left = *(Entries*)a;
// Entries right = *(Entries*)b;
// return (*(int*)left.neighbourhoodName - *(int*)right.neighbourhoodName);
// return (*(int*)(*(Entries**)a)->neighbourhoodName - *(int*)(*(Entries**)b)->neighbourhoodName);
// Entries *left = *(Entries**)a;
// Entries *right = *(Entries**)b;
// return (*(int*)left->neighbourhoodName - *(int*)right->neighbourhoodName);
Code:
Entries **ent = DB->picnicTableTable->entry;
qsort(ent,DB->picnicTableTable->size-1, sizeof(Entries*), cmpfunc);