gpt4 book ai didi

c - qsort 的结构数组不工作

转载 作者:行者123 更新时间:2023-12-02 06:06:52 25 4
gpt4 key购买 nike

我正在尝试对一个名为 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;
}

上面的输出只是按照最初放置的顺序排列的名称列表

最佳答案

int count_number_of_different_persons(run results[])

这并不能真正让您在数组上使用 sizeof,因为数组已衰减为指针。

这个

run *run1 = *(run **)a;

看起来也很奇怪,不应该吗

run *run1 = (run*)a;

?

关于c - qsort 的结构数组不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8354502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com