pointers - EXC_BAD_ACCESS using QSort on an Array of Chars within Structs in C -
i have searched through many of answers on here , have implemented few changes based on that, getting exc_bad_access error when calling qsort function. ide pointing return in qsort compare function problem. know allocating memory elements can print strings no problem if omit call qsort. point me in right direction?
my structs, see how deep navigating:
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; entries **ent = db->picnictabletable->entry; qsort(ent,db->picnictabletable->size-1, sizeof(entries*), cmpfunc); 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;
this call looks like:
entries **ent = db->picnictabletable->entry; qsort(ent,db->picnictabletable->size-1, sizeof(entries*), cmpfunc);
and compare function is:
int cmpfunc(const void *a, const void *b) { entries *left = *(entries**)a; entries *right = *(entries**)b; return strcmp(left->neighbourhoodname, right->neighbourhoodname); }
the picnictabletable, , entry initialized after malloc:
db->picnictabletable = malloc(sizeof(picnictable)); db->picnictabletable->entry = malloc(numentries*sizeof(entries)+1); db->picnictabletable->size = numentries; while ((c=fgetc(in)) != eof) { if (c == ',' && row > 0) { switch (column) { case 0: neighbourhoodname = copytochar(buff, begin, i); ... }
copytochar take slice of buffer , allocates memory returns pointer value assign:
char * copytochar(const char * buff, int begin, int end) { char *temp = malloc(end - begin + 1); int j = 0; (int = begin; < end; i++, j++) temp[j] = buff[i]; temp[j] = '\0'; return temp; }
and array populated after iterate through each row in file (this 1 entry):
db->picnictabletable->entry[row]->neighbourhoodname = malloc(strlen(neighbourhoodname)*sizeof(char)+1);
the values of a->neighbourhoodname null, confuses me. doesn't qsort pass 2 values array compare function?
thanks time!
beware. declare entry pointer pointers (pointing first element of array of pointers)
typedef struct { int size; entries **entry; } picnictable;
but initialize mere dynamic array, pointer first element of array of entrys
in:
db->picnictabletable->entry = malloc(numentries*sizeof(entries)+1);
if want array of pointers speed qsort, should build separately:
typedef struct { int size; entries *entry; entries **pentry; } picnictable; db->picnictabletable->entry = malloc(numentries*sizeof(entries)+1); db->picnictabletable->pentry = malloc(numentries*sizeof(&entries)+1); (int i=0; i<=numentries; i++) { // initialize array of pointers pentry[i] = entry + i; }
currently definition of cmpfunc
not coherent initialization of db->picnictabletable->entry
.
Comments
Post a Comment