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 );
}
My Structure
struct tod {
int hour, minute;
};
struct event {
struct tod start, end;
};
Just using compare
instead of compare()
, the compiler seems to treat it as a variable instead.
Secondly, I'm wondering if my compare function is correct? Since i'm getting some errors from the compiler, more specifically the following
Error: request for member 'start' in something not a structure or union
^ that error occurs for this line int startA = evA.start.hour*60 + evA.start.minute;
So I assume it thinks that evA is not a structure even though I explicitly declared it as such. This might be because I haven't properly declared it, any help would be appreciated :)
由于evA
和evB
是指针,因此必须使用evA->member
而不是evA.member
。
You should use ->
to reference the structure member if you're doing this with pointers:
int startA = evA->start.hour*60 + evA->start.minute;
int startB = evB->start.hour*60 + evB->start.minute;
And your call to qsort
should be:
qsort(schedule, n, sizeof(struct event), compare);
since the fourth argument is a pointer to a function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.