我尝试按char对名为results的struct run数组进行排序,但当我打印该数组时,没有排序。看看这个:
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;
}上面的输出只是按最初放置顺序排列的名称列表
发布于 2011-12-02 17:57:17
int count_number_of_different_persons(run results[])这并不能真正让你在数组上使用sizeof,因为数组会衰减成指针。
这
run *run1 = *(run **)a;看起来也很奇怪,不是吗?
run *run1 = (run*)a;https://stackoverflow.com/questions/8354502
复制相似问题