arrays - 如何使用 qsort 对结构体指针数组进行排序

标签 arrays c pointers struct qsort

这是我的 Book 结构,这是我的代码:

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;
}

但是qsort不起作用 选择4号菜单,该程序停止。 我尝试调试,发现a和b有未知值。 并且打印排序结果的printf语句发生EXC_BAD_ACCESS错误。 我需要做什么来排序。

最佳答案

应该初始化数组books

BOOK *books[MAX_SIZE] = { NULL };

否则这个循环

for (int i = 0; i < MAX_SIZE; ++i) {
    free(books[i]);
    books[i] = NULL;
}

将调用未定义的行为。

由于您有一个指针数组,因此 qsort 的调用至少应该类似于

qsort( books, count, sizeof( BOOK * ), comparePrice );

另一方面,函数 comparePrice 应按以下方式定义

int comparePrice( const void *bookA, const void *bookB ) 
{
    const BOOK *a = *( const BOOK * const * ) bookA;
    const BOOK *b = *( const BOOK * const * ) bookB;
    
    return ( a->price < b->price ) - ( b->price < a->price );
}

这是一个简化的演示程序。

#include <stdio.h>
#include <stdlib.h>

typedef struct book
{
    int price;
} BOOK;

int comparePrice( const void *bookA, const void *bookB ) 
{
    const BOOK *a = *( const BOOK * const * ) bookA;
    const BOOK *b = *( const BOOK * const * ) bookB;
    
    return ( a->price < b->price ) - ( b->price < a->price );
}

int main(void) 
{
    BOOK b1 = { 10 }, b2 = { 20 };
    BOOK *a[] = { &b1, &b2 };
    const size_t count = sizeof( a ) / sizeof( *a );
    
    for ( size_t i = 0; i < count; i++ )
    {
        printf( "%d ", a[i]->price );
    }
    
    putchar( '\n' );

    qsort( a, count, sizeof( BOOK * ), comparePrice );
    
    for ( size_t i = 0; i < count; i++ )
    {
        printf( "%d ", a[i]->price );
    }
    
    putchar( '\n' );
    
    return 0;
}

程序输出为

10 20 
20 10

关于arrays - 如何使用 qsort 对结构体指针数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67491617/

相关文章:

arrays - Elasticsearch Kibana中的映射数组(文本,数据)

.net - .NET 中的只读数组

android - 是否可以在 native C 中读取 logcat?

c - %.*s 在这个程序中有什么用

复制一维元素二维数组

c++ - R:指向c函数的指针

无法增加取消引用的指针的值

Javascript - 数组在原型(prototype)中未定义?

arrays - Swift appendContentsOf(顺序)

即使在缓冲区完全填满之前,C 中已满缓冲的输出流是否可以自动刷新?