在c中的结构指针数组上使用qsort时比较整数的函数
stackoverflow 上有很多关于如何对结构指针数组进行排序的问题。我把它们都翻遍了,没有结果。我想对指向结构数组的指针数组进行排序。我首先为指针数组分配存储空间,然后为结构本身分配存储空间。一切看起来都很好,但我无法将它们排序。我确信问题出在比较函数中。我从 stackoverflow 复制了其中一些,它们列在下面。但它们都不起作用...
typedef struct s_stream{
int amc;
char *name;
} dataStream;
void abc(void)
{
int count = 100;
dataStream *_UniqueStreamBuild = calloc(count, sizeof(dataStream ));
dataStream **UniqueStreamBuild = calloc(count, sizeof(dataStream *));
for ( int i = 0; i < count; ++i) UniqueStreamBuild[i] = _UniqueStreamBuild + i;
//**Edit: ******** **
// here I call a cascade of functions that assign values to amc; those
// functions are correct: they produce an unsorted array of amc values;
// the output I am getting is an array of structures seemingly in random order.
qsort(UniqueStreamBuild, count, sizeof(dataStream *), compare);
}
int compare (const void * a, const void * b)
{
const dataStream *x = a;
const dataStream *y = b;
if (x->amc > x->amc)
return(1);
if (x->amc < x->amc)
return(-1);
return(0);
}
int compare( const void *a, const void *b )
{
dataStream *m1 = *(dataStream **)a;
dataStream *m2 = *(dataStream **)b;
if (m1->amc > m2->amc)
return(1);
if (m1->amc < m2->amc)
return(-1);
return(0);
}
There are lots of questions on stackoverflow regarding how to sort an array of structure pointers. I looked through them all, to no avail. I want to sort an array of pointers to an array of structures. I first allocate storage for the pointer array, then for the structures themselves. All that seems fine, but I can't get them sorted. I'm sure the problem is in the compare function. I've copied a few of them from stackoverflow, and they are listed below. But none of them work...
typedef struct s_stream{
int amc;
char *name;
} dataStream;
void abc(void)
{
int count = 100;
dataStream *_UniqueStreamBuild = calloc(count, sizeof(dataStream ));
dataStream **UniqueStreamBuild = calloc(count, sizeof(dataStream *));
for ( int i = 0; i < count; ++i) UniqueStreamBuild[i] = _UniqueStreamBuild + i;
//**Edit: ******** **
// here I call a cascade of functions that assign values to amc; those
// functions are correct: they produce an unsorted array of amc values;
// the output I am getting is an array of structures seemingly in random order.
qsort(UniqueStreamBuild, count, sizeof(dataStream *), compare);
}
int compare (const void * a, const void * b)
{
const dataStream *x = a;
const dataStream *y = b;
if (x->amc > x->amc)
return(1);
if (x->amc < x->amc)
return(-1);
return(0);
}
int compare( const void *a, const void *b )
{
dataStream *m1 = *(dataStream **)a;
dataStream *m2 = *(dataStream **)b;
if (m1->amc > m2->amc)
return(1);
if (m1->amc < m2->amc)
return(-1);
return(0);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的第二个可能的
compare()
函数应该可以工作,除非它与下面的这个版本之间存在一些我没有注意到的差异。对指针数组进行排序时,比较函数会传递两个指向 dataStream * 的指针,因此比较器应该与此非常相似:此外,正如最初所写,您的函数之一始终返回 0,因为
x->amc == x->amc
(取消引用x
两次,而不是x
和y
>)。您的测试代码没有完全初始化数据结构 - 它使用
calloc()
,因此结构中的字符串和指针都归零,因此排序作用不大。这段代码对我有用...你呢?
Your second possible
compare()
function should work unless there's some difference I didn't notice between it and this version just below. When sorting an array of pointers, the comparison function is passed two pointers to adataStream *
, hence the comparator should be very similar to this:Also, as originally written, one of your functions always returns 0 because
x->amc == x->amc
(you dereferencex
twice, instead ofx
andy
).Your test code does not fully initialize the data structures - it uses
calloc()
so the strings and pointers in the structures are all zeroed, so sorting doesn't do much.This code works for me...how about you?