qsort() - HELP

Johan Harsta jha at spodv5.UUCP
Wed Feb 14 00:10:46 AEST 1990


In article <5916 at ozdaltx.UUCP>, root at ozdaltx.UUCP (root) writes:
> After going over the manuals more times than I'd like, I still can't
> figure out how to get qsort() (S) to operate.  The example shows
> something like:
> 
> 	void qsort(base, nel, width, compar)
> 	char *base
> 	unsigned nel, width
> 	int (*compar)()
> 
> Is base supposed to be an array?
	- 'base' should be a pointer to the base of the table, and of type
	pointer to an element in the table, casted to a pointer to character
> nel & width are self-explanitory
> What is compar() looking for or how is it assigned?
	- 'compar' is supplied by you; e.g. 'strcmp' or a modified version
	of it

E.g.:
	struct ELEMENT buffer[nele];

	sort_buf (&buffer[0], nele);

	void
	sort_buffer (base, nele)
		struct ELEMENT *base;
		int nele;
	{	
		qsort ((char *) base, 
			(unsigned) nele, 
			(unsigned) sizeof (struct ELEMENT),
			sorting_alg);
		return;
	}

	int
	sorting_alg (arg1, arg2)
		char *arg1;
		char *arg2;
	{
		return (strncmp (arg1, arg2, MAX_RELEVANT_CHARACTERS));
	}

	Something like the above should work, good luck!	

	/Johan Harsta, Programator Uppsala AB, Sweden (consultant for Philips)



More information about the Comp.lang.c mailing list