int priority;
} symb_t;
symb_t* new_symb(char ch, int priority) {
symb_t *out = malloc(sizeof(symb_t));
out->c = ch;
out->priority = priority;
return out;
}
int compare_priority(const void *m1, const void *m2) {
symb_t *A = m1;
symb_t *B = m2;
int a = A->priority;
int b = B->priority;
if (a > b) { return +1; }
if (a < b) { return -1; }
return 0;
}
void print_symb(symb_t **s, int len) {
printf("[");
for (int i = 0; i < len; i++) {
printf("[%p | '%c', %d]", s[i], s[i]->c, s[i]->priority);
if (i + 1 != len)
printf(", ");
}
puts("]\n");
}
int main() {
const int lensymb = 10;
symb_t **symbols = (symb_t**)calloc(lensymb, sizeof(symb_t*));
symbols[0] = new_symb('f', 0);
symbols[1] = new_symb('q', 9);
symbols[2] = new_symb('w', 3);
symbols[3] = new_symb('d', 2);
symbols[4] = new_symb('r', 8);
symbols[5] = new_symb('t', 7);
symbols[6] = new_symb('e', 1);
symbols[7] = new_symb('n', 6);
symbols[8] = new_symb('y', 4);
symbols[9] = new_symb('k', 5);
print_symb(symbols, lensymb); fflush(stdout);
qsort(*symbols, lensymb, 32, compare_priority); /// !!! WTF ??
print_symb(symbols, lensymb);
}
pastebin.com
Обсуждают сегодня