结构数组的qsort不工作 [英] qsort of struct array not working
本文介绍了结构数组的qsort不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图整理了一个名为由一个char结果结构来看阵列,但是当我打印数组,没有什么是排序。看看这个:
结构运行{
炭名称[20],平日[4],月[10];
(和一些更多...)
};
typedef结构跑跑;INT name_compare(常量无效*一,常量无效* B)
{
运行* RUN1 = *(运行**)一个;
运行* RUN2 = *(运行**)B:
返回STRCMP(run1->的名字,run2->名);
}INT count_number_of_different_persons(运行结果[])
{
INT I = 0;
的qsort(结果的sizeof(结果)/的sizeof(运行),的sizeof(运行),name_compare); 对于(i = 0; I< = 999;我++)
{
([I]。名称%S \\ n,结果)printf的;
}
//不使用此功能尚未完成,只返回0
返回0;
}
从上述的输出仅有它们最初被放置在顺序的名称列表
解决方案
INT count_number_of_different_persons(运行结果[])
这并没有真正让你用的sizeof
阵列上,因为阵列衰减到指针。
这
运行* RUN1 = *(运行**)一个;
看起来也怪异,应该不会是
运行* RUN1 =(运行*)一个;
I am trying to sort a struct run array called results by a char, but when I print the array, nothing is sorted. Have a look at this:
struct run {
char name[20], weekday[4], month[10];
(And some more...)
};
typedef struct run run;
int name_compare(const void *a, const void *b)
{
run *run1 = *(run **)a;
run *run2 = *(run **)b;
return strcmp(run1->name, run2->name);
}
int count_number_of_different_persons(run results[])
{
int i = 0;
qsort(results, sizeof(results) / sizeof(run), sizeof(run), name_compare);
for(i = 0; i <= 999; i++)
{
printf("%s\n", results[i].name);
}
// not done with this function yet, just return 0
return 0;
}
The output from the above is just a list of names in the order they were originally placed
解决方案
int count_number_of_different_persons(run results[])
This doesn't really let you use sizeof
on the array, because array is decayed to pointer.
This
run *run1 = *(run **)a;
also looks weird, shouldn't it be
run *run1 = (run*)a;
?
这篇关于结构数组的qsort不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文