# Thread: qsort problems... help!

1. ## qsort problems... help!

Hi im new to this board but ive heard great things about it, and hopefully someone can help me out here:

Im trying to use the built in c function qsort to sort my table of characters and their frequencies (writting the Huffman's Algorithm) . But for some reason my code core dumps inside my compare function. I debugged it and it says the value of a is a null pointer. Here's my struct, tablecount for how many elements, and the freqTable:

typedef struct tableItem {
char myChar;
int freq;
} tableItem;

static int tableCount = 0;

tableItem** freqTable;

Here is my compare function:

int compareT(const void *a,const void *b)
{
tableItem * const *first = a;
tableItem * const *second = b;

int retVal;

retVal = (*second)->freq - (*first)->freq;
return retVal;
}

And here is how i am calling qsort:
...
void* tblptr = freqTable;
...

qsort(tblptr, tableCount-1, sizeof(tableItem*), compareT);

...

I print out all the items, qsort it, then print out again, and here is as far as my output goes:

[a:3]
[b:1]
[c:2]
[f:3]
[z:1]
[u:2]
[r:6]
[e:1]
-------------
Segmentation fault (core dumped)

I debugged and it tells me i am seg faulting inside compareT at line:

retVal = (*second)->freq - (*first)->freq;

Any help will be appreciated.. thanks!

2. Ok.. here's the whole program:
(I know my checks for NULL after malloc and realloc are screwy, but i dont think that would be the cause of the problem.)
Code:
```#include <stdlib.h>
#include <stdio.h>

typedef struct tableItem {
char myChar;
int freq;
} tableItem;

static int tableCount = 0;

tableItem** freqTable;

/*returns index in freqTable of either next open spot or where current character is */

int findInTable(char toFind)
{
int index = 0;

while(index < tableCount && freqTable[index] != NULL)
{
if(freqTable[index]->myChar == toFind)
break;
else
index++;
}
return index;
}

/*adds character to table by getting index from findInTable*/
{

int where = 0;
void* temp;

if((temp = realloc(freqTable, tableCount*sizeof(tableItem*) + sizeof(tableItem*))) == NULL)
{
free(freqTable);
printf("screw it");
exit(1);
}
freqTable = temp;
if(freqTable[where] == NULL)
{
if((temp = malloc(sizeof(tableItem))) == NULL)
{
free(freqTable);
printf("screw it");
exit(1);
}
freqTable[where] = temp;
freqTable[where]->freq = 1;
tableCount++;
}
else
{
freqTable[where]->freq++;
}
}

void printTable()
{
int index = 0;
tableItem* temp;

while(index < tableCount && freqTable[index] != NULL)
{
temp = freqTable[index];
printf("[%c:%d]\n", temp->myChar, temp->freq);
index++;
}
}

/* ok i commented out that part and just made it one line of code, it works but qsort is switching all the wrong things, doesn't sort it.  Ill put the output at the bottom */

int compareT(const void *a,const void *b)
{
/*tableItem * const *first = a;
tableItem * const *second = b;

int retVal;

retVal = *(*second).freq - *(*first).freq;

return retVal;*/

return ((*(tableItem*)a).freq - (*(tableItem*)b).freq);
}

int
main(int argc, char **argv)
{

printTable();
printf("-------------\n");

qsort((void *)freqTable, tableCount-1, sizeof(freqTable[0]), compareT);

printTable();

return 0;
}```
Ok now the program doesn't core dump, but it doesn't sort either, here is my output:

[a:3]
[b:1]
[c:2]
[f:3]
[z:1]
[u:2]
[r:6]
[e:1]
------------- /*sort occurs here */
[c:2]
[b:1]
[a:3]
[z:1]
[r:6]
[u:2]
[f:3]
[e:1]

thanks for any help!!

3. ((*(tableItem**)a)->freq - (*(tableItem**)b)->freq);

4. If i do that, then i get this error at compilation:

hw6.c:205: invalid type argument of `->'
hw6.c:205: invalid type argument of `->'
hw6.c:207: warning: control reaches end of non-void function

5. Also im compiling with these options:

gcc -ansi -pedantic -Wall -O2

if that helps.. thanks for any replies

6. Thank you so much!!! Works perfectly! I like the way you did it much much better Thanks again

Popular pages Recent additions