using qsort... (nv, fixed it)
Posted: Sun Apr 13, 2003 2:26 pm
what am I doing wrong? (using a std qsort)<br><br>I have a struct:<br>struct s<br>{int a,b,c};<br><br>and s1 > s2 iff s1.c > s2.c .:<br>int sCmp (const s *s1, const s *s2)<br>{<br> return (s1->c < s2->c);<br>}// is s1 < s2?<br><br>and then I create the cmp function ptr:<br>int cmp(const void *v1, const void *v2)<br>{// cast v1 & v2 as type 's' and run sCmp<br> return sCmp((s*)v1, (s*)v2);<br>}<br><br>and my qsort call:<br>qsort(data, size, sizeof(s), cmp);<br>// data = 's *data', size = num elements in data, sizeof(s) = width of an array element, cmp = comparison function<br><br>*edit* forgot to describe the problem...<br>it compiles and runs... just it doesn't sort... the elements are now in pretty much a random order! <!--EZCODE EMOTICON START :( --><img src=http://www.ezboard.com/images/emoticons/frown.gif ALT=":("><!--EZCODE EMOTICON END--> <br>it works fine up through 7 items... after that it all seems to go to pot...<br><br>*edit* LOL should have been returning 1 or -1, not 0 and 1 <!--EZCODE EMOTICON START :) --><img src=http://www.ezboard.com/images/emoticons/smile.gif ALT=":)"><!--EZCODE EMOTICON END--> <p><!--EZCODE FONT START--><span style="color:red;font-size:large;">C</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:orange;font-size:large;">O</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:green;font-size:large;">L</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:blue;font-size:large;">O</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:indigo;font-size:large;">R</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:violet;font-size:large;">S</span><!--EZCODE FONT END--><!--EZCODE FONT START--><span style="color:pink;font-size:xx-large;">!</span><!--EZCODE FONT END--></p><i>Edited by: <A HREF=http://pub141.ezboard.com/bxmenclan.sho ... ckRider</A> at: 4/13/03 4:19:48 pm<br></i>