#include #include #include #ifndef NULL #define NULL (void *)0 #endif #define SIZ 20 typedef struct { char c; int n; } ncs; ncs* ncsMake() { ncs *ncsp = malloc(1 * sizeof(ncs)); ncsp->c = 'A' + rand() % 26; ncsp->n = rand() % 100; return ncsp; } int ncsCompareC(const void * p, const void * q) { const ncs **ppp = (const ncs **) p; const ncs **qpp = (const ncs **) q; const ncs *pp = *ppp; const ncs *qp = *qpp; return pp->c - qp->c; } void ncsDestroy(ncs * ncsp) { free(ncsp); } int ncsCompareN(const void * p, const void * q) { const ncs **ppp = (const ncs **) p; const ncs **qpp = (const ncs **) q; const ncs *pp = *ppp; const ncs *qp = *qpp; return pp->n - qp->n; } void printr(ncs** ncspp) { for (int i = 0; i < SIZ; i++) { printf("<%c %2d> ", ncspp[i]->c, ncspp[i]->n); } printf("\n"); } int main(int argc, char const *argv[]) { srand(time(NULL)); ncs **ncspp = malloc(SIZ * sizeof(ncs *)); for (int i = 0; i < SIZ; i++) ncspp[i] = ncsMake(); printr(ncspp); qsort(ncspp, SIZ, sizeof(ncs *), ncsCompareN); printr(ncspp); for (int i = 0; i < SIZ; i++) ncsDestroy(ncspp[i]); free(ncspp); return 0; }