This is my Book
structure and here is my code:
typedef struct book {
char title[100];
char author[20];
int price;
} BOOK;
#define MAX_SIZE 10
int comparePrice(const void *bookA, const void *bookB);
int count = 0;
int main() {
BOOK *books[MAX_SIZE]; // here is array of struct pointers
while (1) {
char title[100] = "";
char author[20] = "";
int price = -1;
//////////// printf select menu ////////////
int selector = -1;
scanf("%d", &selector);
switch (selector) {
case 1:
while (getchar() != '\n');
printf("--------input book data--------\n");
printf("title :");
gets(title);
printf("author :");
gets(author);
printf("price :");
scanf("%d", &price);
books[count] = (BOOK *) malloc(sizeof(BOOK));
memset(books[count], 0, sizeof(BOOK));
strcpy(books[count]->title, title);
strcpy(books[count]->author, author);
books[count]->price = price;
count++;
break;
case 4:
printf("--------sorting price--------\n");
qsort(books, count, sizeof(BOOK), comparePrice);
for (int i = 0; i < count; ++i) {
printf("%d. title: %s author: %s price: %d\n", i + 1, books[i]->title, books[i]->author, books[i]->price);
}
break;
default:
for (int i = 0; i < MAX_SIZE; ++i) {
free(books[i]);
books[i] = NULL;
}
return 0;
}
}
}
int comparePrice(const void *bookA, const void *bookB) {
const BOOK *a = (const BOOK *)bookA;
const BOOK *b = (const BOOK *)bookB;
return b->price - a->price;
}
but qsort
is not working
select number 4 menu and this program stop.
I tried debugging and found that a and b have unknown values.
And EXC_BAD_ACCESS
error occurs in the printf statement to print the sorting result.
What I need to do for sorting.