我需要根据属性对结构进行排序。我做了一个compare函数来做到这一点,但问题是该数组可以具有NULL值,并且会导致错误。这是我的代码的相关部分:
#define MAX_NUM 5
struct d {
int id;
int p;
int prio;
};
struct d *P[MAX_NUM];
int compare(const void *p, const void *q)
{
int l = ((struct dretva *)p)->prio;
int r = ((struct dretva *)q)->prio;
if (l < r) return -1;
else if (l > r) return 1;
else return 0;
}
我这样调用qsort函数:
qsort(P, MAX_NUM, sizeof(d), compare);
我认为这应该在正常情况下有效,但是由于数组P具有NULL值,所以没有。我该如何进行这项工作?
Edit: to be specific, whit this code I get Segmentation fault (core dumped)