I am attempting to understand the c library qsort in the context of pointers to structs. Here is the existing code that I would like to manipulate:
The structure:
#define MAX_NAME 20
#define NUM_MONTHS 12
typedef struct EMP {
char name[MAX_NAME+1];
int monthSales[NUM_MONTHS];
int total;
} Emp;
The global initialization of the data and its size:
Emp *data;//where all entries are kept
int empSize;
and I have constructed 2 arrays of Emp pointers that I would like refer to the data in different orders:
Emp *nameArray[empSize];//an array of pointers to point to entries alphabetically
Emp *salesArray[empSize]; //an array of pointers to pointing to entries by sales
after they have been identically assigned, I would like to use a qsort to arrange them differently. the nameArray alphabetically, using the name in the struct and the salesArray largest to smallest, using the total in the struct
What should the compare methods and the qsort arguments look like?
Thanks
Emp data[empSize];to make it a bit easier to get your head around it? I'd start with an array ofthe structs, get that working, then use the array of pointers. Then write the scond comparison function, and use the secoond array of pointers.