Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!spool.mu.edu!snorkelwacker.mit.edu!hsdndev!cmcl2!adm!smoke!gwyn From: gwyn@smoke.brl.mil (Doug Gwyn) Newsgroups: comp.lang.c Subject: Re: HOW does "qsort" handle different variable type? Message-ID: <15561@smoke.brl.mil> Date: 25 Mar 91 08:02:05 GMT Article-I.D.: smoke.15561 References: <1991Mar23.063112.22168@minyos.xx.rmit.oz.au> Organization: U.S. Army Ballistic Research Laboratory, APG, MD. Lines: 21 In article <1991Mar23.063112.22168@minyos.xx.rmit.oz.au> rcoahk@chudich.co.rmit.oz (Alvaro Hui Kau) writes: > I am interested in HOW the system routine > handle different variable types.. > Furthermore, WHY char casting is used for > the base pointer? I think your question boils down to, "Explain the function of the first argument to the qsort() function." qsort()'s first argument is a "generic" pointer, formerly of type char*, in ANSI C of type void*. The reason for this is of course that the implementation of qsort() has no way of knowing what the actual data type will be at run time. The third argument to qsort() specifies the byte-width of a single element in the array of whatever actual type is being sorted, so that qsort() is able to construct generic pointers to the proper elements that it passes to the user-supplied comparison function. qsort() manages the sorting algorithm, while the user- supplied comparison function gives meaning to comparison of whatever data types are actually being sorted. Note that the comparison function has generic-pointer types for its arguments, which must be converted to pointers to the actual data types being compared.