Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!ira.uka.de!smurf!altger!doitcr!de.intel.com!intelhf!ichips!iwarp.intel.com!inews!nevin!bhoughto From: bhoughto@nevin.intel.com (Blair P. Houghton) Newsgroups: comp.lang.c Subject: Re: HOW does "qsort" handle different variable type? Message-ID: <3421@inews.intel.com> Date: 24 Mar 91 02:34:19 GMT References: <1991Mar23.063112.22168@minyos.xx.rmit.oz.au> Sender: news@inews.intel.com Organization: Intel Corp, Chandler, AZ Lines: 31 In article <1991Mar23.063112.22168@minyos.xx.rmit.oz.au> rcoahk@chudich.co.rmit.oz (Alvaro Hui Kau) writes: >Hi, > Furthermore, WHY char casting is used for > the base pointer? Even in ANSI C, you can expect a pointer to char to be a pointer to a byte. This makes it a natural choice for a generic pointer type. It means that when the pointer is incremented by 1, the location it points to will be one byte further in the memory. Notice that you have to give also the number of bytes that each object occupies. qsort() uses that number to increment the pointer and thus point to the next object, not just the next byte. In ANSI C, however, there's a special type, `void *', which is designed specifically to be a generic pointer type, to be used in exactly this way. qsort()'s sorting algorithm often isn't the best [*] (the algorithm you use should depend on the distribution and organization of your data, so it's easy to define a class of sets that make a given sorting algorithm thrash and slog), but this usage of anonymous data methods is a classic of modular programming. --Blair "Waxing didactic this week..." [*] the algorithm it uses can be different from library to library: anything from bubble-sort to hashing.