【发布时间】:2013-04-20 06:57:09
【问题描述】:
所以我在使用 qsort 对结构数组进行排序时遇到了麻烦。
我以这个链接为例:http://support.microsoft.com/kb/73853
当我运行程序时,它会为结构中的原始名称提供空白,并为 gp 的所有值提供零。
typedef int (*compfn)(const void*, const void*);
struct record
{
char player[20];
int gp;
};
struct record entries[15];
int compare(struct record *, struct record *);
void show ()
{
int v;
qsort((void *)entries, 10, sizeof(struct record), (compfunc)compare);
struct record *p = entries;
for(v=0;v<counter;v++, p++)
{
printf("%s ..... %d \n", p->player , p->gp);
}
}
int compare(struct record * p1, struct record * p2)
{
if( p1->gp < p2->gp)
return -1;
else if (p1->gp > p2->gp)
return 1;
else
return 0;
}
编辑:大家好,非常感谢你们的帮助,但是,我已经尝试了你们所说的一切,它仍然只是将所有值都归零
【问题讨论】:
-
这不应该编译。
-
除了 (compfunc) 到 (compfn) 它对我有用