I'm programming in C using gcc. I've used qsort() successfully many times before under Turbo C, but this is the first time for gcc.
One difference is that Turbo C uses 2-byte short ints, but gcc uses 4-byte short ints. I thought gcc might be inserting slack bytes in the ELEMEN structure, but I added the ((packed)) attribute and this did not fix the problem.
Perhaps my call to the qsort() function is wrong. This particular problem is not mentioned on the internet. I checked the syntax of the call to qsort() there and it seems to be correct. There's nothing else to check there.
I'm including (what I believe to be) the relevant portions of my program. Let me know if you need anything else.
Code: Select all
#include <stdlib.h> /* 5 */
#define NOSLACK __attribute__((packed))
/* don't insert slack bytes (comp without sync) */
/* 10 */
#define TABWIDTH 200
/* width of table of tasks, prereqs, and postreqs (task table) */
/* 41 */
#define TABSIZE 100
/* number of entries in task table */
#define FIXED 3 * sizeof(short int) + 2 * sizeof(char)
/* size of fixed-length portion of task table */
struct ELEMEN /* 59 */
{short int tasknum; /* task number of THIS task (excluding any step number) */
char stepnum; /* step number (first = 'a', etc.) */
char flags; /* flags pertaining to this task */
short int numpre; /* number of prereqs this task has */
short int numpost; /* number of postreqs this task has */
short int prereqs[0]; /* table of prerequisites (omitted if numpre = 0) */
short int postreqs[0]; /* table of postrequisites (omitted if numpost = 0) */
char fragment[TABWIDTH /* fragment of the description; the max- */
- FIXED];}; /* imum that will fit (with zero byte) */
/* the lengths of all the above fields should total to TABWIDTH */
/* 70 */
struct GLOBALS /* some global variables follow: */
{NOSLACK struct ELEMEN cactus[TABSIZE];
. . .
char workarea[600];} g; /* input buffer */
int e35(const void*, const void*);
qsort(&g.cactus, g.numelem, TABWIDTH, e35); /* sort cactus using e35() */
int e35(const void* elem1, const void* elem2)
{return (((const struct ELEMEN*)elem1)-> tasknum
> ((const struct ELEMEN*)elem2)-> tasknum) /* 395 */
- (((const struct ELEMEN*)elem1)-> tasknum
< ((const struct ELEMEN*)elem2)-> tasknum);};