TIP: Click on subject to list as thread! ANSI
echo: locsysop
to: Bob Lawrence
from: Michael Stapleton
date: 1996-12-18 22:49:48
subject: qsort

Hi Bob,

On Dec 12 07:58, 1996, Bob Lawrence of 3:711/934.12 wrote:

BL> I've finally got around to looking at your C qsort() code and it
BL> works!

Yay!

BL>   I had to correct a few errors before it would compile...

Fair enough - my ability to write portable code has improved over
the last 3 or 4 years.

BL> /*
BL>  *  L I N E S O R T 0
BL>  *
BL>  *  Sort a char array full of nul-terminated strings
BL>  *
BL>  *  Written by M. Stapleton of Graphic Bits
BL>  *
BL>  *  Modified from a Snipet by Bob Lennon
BL>  *
BL>  *  Sep 11 1993
BL>  */

BL> #include 
BL> #include 
BL> #include 
BL> #include 
BL> #include 

BL> /* Compare Sort Elements */
BL> int pscompare(const void *a, const void *b)
BL> ...           ^^^^ it didn't like this missing
BL> {
BL>   return strcmp(*(char **)a,  *(char **)b);
BL> }

BL> #define MAXS 120
BL> #define MAXP MAXS
BL> #define MAXR 26L
BL> /*#define RAND_MAX SHRT_MAX */
BL> ... for some reason it didn't like RAND_MAX defined as SHRT_MAX
BL> ... so I just used SHRT_MAX. You programmers are strange...

Sorry about that - my dumb compiler doesn't have a RAND_MAX. I
should've used #ifndef RAND_MAX.

BL> char strs[MAXS];        /* The null-separated strings */
BL> char *strptrs[MAXP];    /* String pointer table */

BL> #define wrand() (rand() & SHRT_MAX)
BL> ...     ^^^^^^^  it didn't like wrand(z) and rand(z) and I can
BL> ... understand that. I don't like it either.

This was an attempt at a portable rand() that returns a short int
because my rand() returns a 32 bit int.

I don't understand why it complained about the z, though, since
wrand() is not "called" with an argument.

BL> int main(void)
BL> {
BL>   unsigned maxpi, pi, si;
BL>   char ch=0, **dpch;

BL>  /* Fill the string table with random-length alpha strings */
BL>   for (si=0; si   {
BL>     if (ch && 10L*wrand() > 9L*SHRT_MAX)
BL>        ch = '\0';
BL>     else
BL>        ch = 'a' + MAXR * wrand() / SHRT_MAX;
BL>    strs[si] = ch;
BL>    }
BL>   strs[MAXS-1] = '\0';    /* Null terminate the table */

BL>  /* Build string pointer table */
BL>   strptrs[0] = strs;
BL>   for (pi=1, si=0; si     if (!(strs[si]))
BL>       strptrs[pi++] = strs + si + 1;
BL>       maxpi = pi-1;

BL>   printf("\nInitial table:\n");
BL>   for (dpch=strptrs, pi=0; pi     printf("%3d \"%s\"\n", pi, *dpch);

BL>   qsort((void*)strptrs, maxpi, sizeof(strptrs[0]), pscompare);

BL>   printf("\nSorted table:\n");
BL>   for (dpch=strptrs, pi=0; pi     printf("%3d \"%s\"\n", pi, *dpch);

BL>   return EXIT_SUCCESS;
BL> }

BL>   But other than that it works like a charm, no wukkas.

Hey!

BL> You've left me with another problem...  now I've got to find out
BL> why it didn't work for me!

Don't worry, we've all been in that position when first learning the
mysteries of C's qsort(). :)

BL>   Thanks for that.

BL> ... [later]

BL>   Well! It works. I dunno what I did wrong last time, but it works
BL> just like a bought one. In my case, I allocate memory for the 
BL> strings
BL> using malloc() and the last time I did it I could not find 
BL> *anything*
BL> that worked. I must have been mixing up two problems but now it 
BL> works,
BL> it makes sense, and I am no longer confused.

Excellent!

BL>   Thank you, Michael...

My pleasure. :)

Michael Stapleton of Graphic Bits
@EOT:

--- Msged/AM 4.00
* Origin: Graphic Bits (3:711/934.33)
SEEN-BY: 711/934 712/610
@PATH: 711/934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.