如何解决使用 qsort 对结构数组进行排序
我正在编写一个对“bigram 数组”进行排序的程序。基本上,我有一个包含 BigramObjects 的数组。每个对象都包含一个“计数”和“二元组”。我想按“计数”的顺序对我的二元数组进行排序。下面是我试图用来解决这个问题的代码。
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include "bigramArray.h"
typedef struct bigramObject *BigramObject;
struct bigramObject {
char *bigram;
int count;
};
struct bigramArray {
BigramObject *data;
int size;
};
#define NUMBER_OF_CHARACTERS (256)
BigramArray bigramArrayCreate(void) {
BigramArray b = malloc(sizeof(struct bigramArray));
assert(b);
b->size = NUMBER_OF_CHARACTERS * NUMBER_OF_CHARACTERS;
b->data = malloc(sizeof(struct bigramObject) * NUMBER_OF_CHARACTERS * NUMBER_OF_CHARACTERS);
assert(b->data);
for(int i = 0; i < NUMBER_OF_CHARACTERS; i++) {
for(int j = 0; j < NUMBER_OF_CHARACTERS; j++) {
BigramObject obj = malloc(sizeof(struct bigramObject));
assert(obj);
//Store the bigram for this cell
obj->bigram = malloc(sizeof(char) * 2);
assert(obj->bigram);
obj->bigram[0] = i;
obj->bigram[1] = j;
obj->count = 0;
b->data[i * NUMBER_OF_CHARACTERS + j] = obj;
}
}
return b;
}
void bigramArrayDestroy(BigramArray b) {
for(int i = 0; i < NUMBER_OF_CHARACTERS; i++) {
for(int j = 0; j < NUMBER_OF_CHARACTERS; j++) {
free(b->data[i * NUMBER_OF_CHARACTERS + j]->bigram);
free(b->data[i * NUMBER_OF_CHARACTERS + j]);
}
}
free(b->data);
free(b);
}
int compare(const void *a,const void *b) {
const BigramObject objA = (BigramObject) a;
const BigramObject objB = (BigramObject) b;
return objA->count - objB->count;
}
void sortBigramArray(BigramArray b) {
// UP TO HERE IS GOOD
qsort(b->data,b->size,sizeof(BigramObject),compare);
}
然而,当我测试排序时,程序根本不排序任何东西。我已经在一个完全普通的二元数组上运行了 gdb(基本上所有的计数都是 0),我注意到 objA 和 objB 都没有计数 0(它们更接近于像 4215872 这样的数字,并且在我走过了一些)。在 sortBigramArray 函数中,所有计数仍然为 0。有人可以向我解释为什么会发生这种变化吗?
解决方法
compare
函数中的参数是指向元素的指针。所以你需要添加正确的类型转换,然后取消引用指针:
const BigramObject objA = *(BigramObject *)a
const BigramObject objB = *(BigramObject *)b;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。