Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!virtech!cpcahil From: cpcahil@virtech.UUCP (Conor P. Cahill) Newsgroups: comp.lang.c Subject: Re: qsorting & managing struct arrays, pointer arrays Keywords: can qsort accept array of pointers to struct? Message-ID: <1298@virtech.UUCP> Date: 20 Oct 89 23:33:47 GMT References: <169@cica.cica.indiana.edu> Distribution: na Organization: Virtual Technologies Inc Lines: 41 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? If yes, how then > can the declaration of ncmp (the compare function) be changed > to accept two elements ([i], [j]) from the array of pointers, Your example would be changed to something like: typedef struct struct_name {...} struct_name; struct_name *list[MAXSIZE], **s_start, *s_one, *s_two; ^ ^ main() { /*allocate and load the array of struct here*/ ^^^^^^^^^^^^ s_start = list; /*other business*/ qsort( s_start, s_members, sizeof( struct_name *), ncmp ); ^ } int ncmp( struct_name ** s_one, struct_name ** s_two ) ^ ^ { return strcmp( (*s_one)->elem, (*s_two)->elem ); ^^^^^^^^ ^^^^^^^^ } NOTE: I have not compiled the above, but you should get the idea. -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+