Subject: Re: qsort problems (belated)
To: Peter Seebach <seebs@solon.com>
From: Jon Armstrong <jma@lpa.com>
List: netbsd-help
Date: 11/25/1996 20:07:07
On Fri, 8 Nov 1996, Peter wrote:

> > (Berndt) writes:
> >> int comparison( TTY_NODE **arg1, TTY_NODE **arg2 )
> 
> > change it to:
> > int comparison(const void *v1, const void *v2)
> > {
> >	TTY_NODE **arg1 = v1, **arg2 = v2;
> 
> Very, very close; unfortunately, you need to cast away the const here,
> or have those be "const TTY_NODE **"'s.
> 
> (This is why I dislike const.)

For all you qsort fans (sorry, I was out of the office):

As long as we're going to cast anyway, try the following dirtyness:

The prototypes:

typedef int (*SPEC)(const void *, const void *);
int comparison(TTY_NODE **, TTY_NODE **);
TTY_NODE *data[NUM_ELEMS];

The call:

qsort(data, NUM_ELEMS, sizeof(data[0]), (SPEC)comparison);

The function:

int comparison(TTY_NODE **arg1, TTY_NODE **arg2)
{
   return something;
}

Ignore the fixed array.  comparison is the focus of the post.

In doing this, you won't have to play games in comparison. You may
use the arguments directly. No assignments or casts.

The drawback is the lack of type checking at the point of the call
to qsort, but we were going to lie one way or the other, anyway.

The above, assuming I made no typo's, keeps the compiler quiet and
is rather easy to read, imo.

Regards... Jon

---------------------------------------------------------------------
 Reply to: armstron@eznet.net - Jon M. Armstrong - LPA Software Inc.
---------------------------------------------------------------------