D
dough
I have a hash table with seperate chaining with a bunch of words in it.
Here is my declaration:
typedef struct word *word;
struct word
{
int count;
char *s;
word next;
};
typedef struct table *table;
struct table
{
int size;
word *table;
};
After I fill the table(t) with words, I want to sort it with qsort().
However, I know my declaration here is wrong somehow:
total(t) gets the total number of words in the table.
here is my call to qsort():
qsort( t->table, total(t), sizeof(word), compare );
here is my compare() function:
int compare(const void *a, const void *b)
{
word wa = malloc(sizeof(wa));
word wb = malloc(sizeof(wb));
wa = *(const word const *) a;
wb = *(const word const *) b;
if( (wb->count) - (wa->count) == 0 )
{
return( strcmp(wb->s, wa->s) );
}
return ( (wb->count) - (wa->count) );
}
Somehow, wb and wa are not getting the right values. wa seems to get an
actual word but wb is left NULL and i end up with a bus error. Any
advice on this matter would be much appreciated.
Here is my declaration:
typedef struct word *word;
struct word
{
int count;
char *s;
word next;
};
typedef struct table *table;
struct table
{
int size;
word *table;
};
After I fill the table(t) with words, I want to sort it with qsort().
However, I know my declaration here is wrong somehow:
total(t) gets the total number of words in the table.
here is my call to qsort():
qsort( t->table, total(t), sizeof(word), compare );
here is my compare() function:
int compare(const void *a, const void *b)
{
word wa = malloc(sizeof(wa));
word wb = malloc(sizeof(wb));
wa = *(const word const *) a;
wb = *(const word const *) b;
if( (wb->count) - (wa->count) == 0 )
{
return( strcmp(wb->s, wa->s) );
}
return ( (wb->count) - (wa->count) );
}
Somehow, wb and wa are not getting the right values. wa seems to get an
actual word but wb is left NULL and i end up with a bus error. Any
advice on this matter would be much appreciated.