gpt4 book ai didi

c - 帮助解决函数中的段错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:42:32 26 4
gpt4 key购买 nike

一段时间以来,我一直在尝试解决这个 bsearch 作业问题。我尝试使用我的代码首先搜索一个条目,如下所示:

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));
}

char *SearchList(char *key, char *list[], size_t num)
{
char **value = bsearch(&key, list, num, sizeof(list[0]), Compare);
return (value == 0 ? 0 : *value);
}

/*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 = SearchList((char *)registrants[0], (char **)attendees, attendeeCount);
}

我的 missedFirstMeeting 似乎可以正确调用单个值,但是当我尝试在循环中重复调用我的 SearchList 函数时:

for (int i = 0; i < attendeeCount; i++) {
*missedFirstMeeting = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);
}

我收到段错误。对我来说,我似乎在做同样的事情,但只是重复调用 SearchList(),但显然有些错误我没有看到,因为我收到了段错误。有任何想法吗?谢谢。

最佳答案

删除 firstMeeting 的前导“*”:

missedFirstMeeting = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);

关于c - 帮助解决函数中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2307343/

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