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



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

您好,我正在用 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?nn");
    char word_remove[25];
    scanf("%s", &word_remove);
    if (strcmp(ind->next->word, word_remove)==0)
    {
        printf("Found:n Word: | Category:n %s | %sn", ind->next->word, ind->next->category);
        printf("Are you sure you want to remove?n1)Yesn 2)Nonn");
        int removing;
        if (removing==1)
        {
            list_els *removed=ind->next;
            ind->next=removed->next;
            free(removed);
        }
        else if (removing==2) {printf("Removing cancelled.nn");}
        else {printf("Wrong operation number!");}
    }
    else printf("Word not available in the base!nn");
    }

}

它向我显示一个错误"结构库"在我使用 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().

最新更新