Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uflorida!haven!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn) Newsgroups: comp.lang.c Subject: Re: qsorting & managing struct arrays, pointer arrays Keywords: can qsort accept array of pointers to struct? Message-ID: <11366@smoke.BRL.MIL> Date: 20 Oct 89 21:58:46 GMT References: <169@cica.cica.indiana.edu> Reply-To: gwyn@brl.arpa (Doug Gwyn) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 13 In article <169@cica.cica.indiana.edu> burleigh@cica.cica.indiana.edu (Frank Burleigh) writes: > Can the standard qsort accept not the array of structures, but > the array of pointers to structure members? qsort() doesn't know what the array it is sorting consists of; it leaves that up to the comparison function. Thus, a standard trick is to have qsort() sort an array of pointers to records rather than an array of records. The rest is merely a matter of getting the type declarations right; this amounts to adding an extra * to the parameters in the comparison function. Note that the comparison function is fed "generic" pointers (void* or char*), and you ought to cast them into record_type**s before using them in the comparison function.