qsorting & managing struct arrays, pointer arrays

Conor P. Cahill cpcahil at virtech.UUCP
Sat Oct 21 09:33:47 AEST 1989


In article <169 at cica.cica.indiana.edu>, burleigh at 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     |
+-----------------------------------------------------------------------+



More information about the Comp.lang.c mailing list