【发布时间】:2013-11-29 01:20:43
【问题描述】:
void sortSchedule (struct event schedule[], int n)
{
qsort(schedule, n, sizeof(struct event), compare());
}
int compare(const void * a, const void * b)
{
const struct event *evA = a;
const struct event *evB = b;
int startA = evA.start.hour*60 + evA.start.minute;
int startB = evB.start.hour*60 + evB.start.minute;
return ( startA - startB );
}
我的结构
struct tod {
int hour, minute;
};
struct event {
struct tod start, end;
};
仅使用compare 而不是compare(),编译器似乎将其视为变量。
其次,我想知道我的比较功能是否正确?由于我从编译器中得到了一些错误,更具体地说是以下
Error: request for member 'start' in something not a structure or union
^ 该行出现错误int startA = evA.start.hour*60 + evA.start.minute;
所以我假设它认为 evA 不是一个结构,即使我明确声明它也是如此。这可能是因为我没有正确声明它,任何帮助将不胜感激:)
【问题讨论】:
-
evA和evB是指针,因此您需要使用evA->start取消引用 -
我之前试过,这是我得到的错误
error: invalid type argument of '->' (have 'const struct tod') -
evA->start.hour*60start 是一个结构,所以你仍然使用.