gpt4 book ai didi

C:列表,结构:错误:结构没有成员

转载 作者:行者123 更新时间:2023-11-30 15:40:16 24 4
gpt4 key购买 nike

英语不是我的母语,对于描述或代码中的任何语法错误,我深感抱歉,我翻译了它以便与您分享。

你好,我正在用 C 语言编写一个小程序,我需要一些帮助,我遇到了一个无法修复的错误,我在论坛和其他任何地方进行了搜索,但我发现没有任何帮助。程序中的其他功能运行良好。

此函数从 txt 文件中读取单词和类别列表,将其放入结构中,创建一个列表。用户输入他想要从文件中删除的单词,因此函数会搜索该单词是否存在,如果存在则删除。

我不是最擅长处理列表的,所以这里可能有一个非常基本、愚蠢的问题,请帮忙吗?

void REMOVE_WORD (int howmanylines)
{

FILE *fp;

if ((fp=fopen("words.txt", "r+w"))==NULL)
{printf("Error while opening the file!\n");
exit(1);}
else
{
typedef struct base
{
struct base *next;
char word[25];
char category[15];
} list_els;

struct base tab[howmanylines];
int i=0;
while (!feof(fp))
{
fscanf(fp, "%s", &tab[i].word);
fscanf(fp, "%s", &tab[i].category);
i++;
}

fclose(fp);

list_els *head;
list_els *el=(list_els*)malloc(sizeof(list_els));
list_els *ind=head;
while (ind->next)
{
ind=ind->next;
ind->next=el;
}



printf("What word do you want to remove?\n\n");
char word_remove[25];
scanf("%s", &word_remove);
if (strcmp(ind->next->word, word_remove)==0)
{
printf("Found:\n Word: | Category:\n %s | %s\n", ind->next->word, ind->next->category);
printf("Are you sure you want to remove?\n1)Yes\n 2)No\n\n");
int removing;
if (removing==1)
{
list_els *removed=ind->next;
ind->next=removed->next;
free(removed);
}
else if (removing==2) {printf("Removing cancelled.\n\n");}
else {printf("Wrong operation number!");}
}
else printf("Word not available in the base!\n\n");
}

}

它向我显示错误“struct base”在我使用 strcmp 的行中没有名为“word”的成员。

最佳答案

在此片段中

list_els *head;
list_els *el=(list_els*)malloc(sizeof(list_els));
list_els *ind=head;
while (ind->next)
{
ind=ind->next;
ind->next=el;
}

您没有将 ind 初始化为任何有效值。您可能想写 ind = el 而不是 head。另外,不要强制转换 malloc()

关于C:列表,结构:错误:结构没有成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21080132/

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