[英]Help with pointers in C using qsort, bsearch
我在使用某些指针/数组符号时遇到了麻烦。 我有两个列表,正在对其进行排序,然后尝试显示它们。 我在下面的代码中对声明是什么以及原因有3条评论。 我的代码如下:
int Compare(const void *a, const void *b);
void SortStudents(char *studentList[], size_t studentCount)
{
qsort(studentList, studentCount, sizeof(studentList[0]), Compare);
}
int Compare(const void *a, const void *b)
{
return (strcmp(*(char **)a, *(char **)b));
}
/*Determines which registrants did not attend the first meeting by searching for registrants
that are not in attendees set. */
void DisplayClassStatus(
const char *registrants[], size_t registrantCount,
const char *attendees[], size_t attendeeCount)
{
char **missedFirstMeeting; // not sure if this is the right declaration
char *start, *end;
// not sure if this is right with the &attendees and registrants for the bsearch()
missedFirstMeeting = bsearch(&attendees, registrants, attendeeCount,
sizeof(attendees[0]), Compare);
printf("Missed First Meeting: \n");
//not sure if this the way to traverse through the array using pointers to display
for (start = missedFirstMeeting, end = &missedFirstMeeting[registrantCount-1]; start < end; ++start) {
printf("%s", *start);
}
}
这似乎是家庭作业,所以我将以一种希望的方式(希望)将您引向正确的方向。
bsearch()
函数在排序列表中搜索一个元素,然后返回其位置或指示未找到该元素的指示符。 上面发布的代码似乎以不同的方式使用bsearch()
。
考虑单独对待每个注册人,并多次使用bsearch()
来查看每个注册人是否在与会者列表中。 如果不是,则显示注册人名称。 不要忘记bsearch()
仅在列表排序后才能正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.