--- Dale Kingston <[EMAIL PROTECTED]> wrote:
[SNIP]
> Maybe you guys can see what I did wrong:
There are 2 flaws in the following bits of code.
> int srt_helps(const void *p1, const void *p2)
> {
> HELP_DATA help1 = *(HELP_DATA *)p1;
> HELP_DATA help2 = *(HELP_DATA *)p2;
>
> if (help1.keyword == NULL) return 2;
> else
> if (help2.keyword == NULL) return 1;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The preceeding 3 lines are the first. The comparison function passed to qsort
should return either a negative or positive number.
I would use:
if (help1.keyword == NULL) return -1;
else
if (help2.keyword == NULL) return 1;
Or even:
if(!help1.keyword && !help2.keyword) return 0;
else if(!help1.keyword) return -1;
else if(!help2.keyword) return 1;
> return ( strcmp( help1.keyword, help2.keyword ) );
> }
>
> void sort_helps(void)
> {
> HELP_DATA *help;
> int i;
>
> for (i = 0, help = help_first; help != NULL; help = help->next, i++);
>
> qsort((void *)help_first, i, sizeof(help_first[0]), srt_helps);
The above line is the second and more critical problem. Qsort does not operate
on linked lists. It operates on arrays. I'm surprised your program doesn't
crash outright when you call this.
The sort function should look more like
void sort_helps(void)
{
int nmemb;
int i;
HELP_DATA **help_array = NULL;
HELP_DATA *help;
/* Count the members in the linked list */
for (nmemb = 0, help = help_first; help != NULL; help = help->next,nmemb++);
return if(!nmemb);
/* Create an array to store them */
help_array= alloc_mem(sizeof(help) * i);
/* Copy the linked list into the array */
for(help = help_first; help != NULL; help = help->next)
help_array[i] = help;
/* qsort the array */
qsort(help_array, nmemb, sizeof(help), srt_helps);
/*Reorganize the list*/
for(i=1, help_first = help = help_array[0]; i < nmemb; i++, help=help->next)
help->next = help_array[i];
help->next = NULL;
}
~Kender
=====
-----BEGIN GEEK CODE BLOCK-----
Version 3.1
GCS/L/C/O d-(+) s++: a-- C+++$>++++ UBLS++++$
P+++(--)$ L+++>++++ E--- W+>++$ N !o K? w(--) !O
M- !V PS+ PE(++) Y+ PGP->+ t+ 5 X+() R(+) tv+@
b++(+++) !DI+++ D G(-) e>+++$ h---() r+++ y+++
------END GEEK CODE BLOCK------
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
http://webhosting.yahoo.com/ps/sb/