0

我正在开发一个 C 程序,它读取一个文本文件,将每一行放在一个 char 数组中,并将该行存储在一个链表节点中。创建链表的过程似乎可行,但是当我调用我的函数时,该函数应该打印存储在链表中的文本而没有任何元音(不要问我为什么要这样做!),我得到一个分段错误...在打印所有没有元音的文本之后。

LinkedList 节点如下所示:

typedef struct linked_list_node link;

struct linked_list_node {
char* word;
link* next;
};

无元音函数如下所示:

void print_no_vowels(link* lines)
{
char vowels[] = "aeiouy";
unsigned int i = 0;
unsigned int j = 0;
unsigned int check = 0;
link* first = lines;

while(first != NULL)
{
    printf("%p\n", first);
    for(i = 0; i < strlen(first->word)-1; i++)
    {
        for(j = 0; j < strlen(vowels)-1; j++)
        {
            if(first->word[i] == vowels[j])
            {
                check = 1;
            }
        }
        if(check != 1)
        {
            printf("%c", first->word[i]);
        } else {
            check = 0;
        }
    }
    printf("\n");

    first = first->next;
}
}

在 printf-ing 很多之后,我得出的结论是程序再次进入 print_no_vowels 函数中的 while 循环,即使在第一个指针应该指向 NULL 之后。我不知道为什么会发生这种情况——在我使用相同方式遍历链表的所有其他函数中,当第一次 == NULL 时,程序不会再次进入 while 循环。

任何帮助表示赞赏,在此先感谢!


编辑:

这是链表的init函数。

link* read_file(FILE *input)
{
char line[100];
link *first = malloc(sizeof(link));
first->word = NULL;
link *curr = NULL;
while(fgets(line, 100, input) != NULL)
{
    if(first->word == NULL)
    {
        first->word = malloc(100);
        strcpy(first->word, line);
        first->next = malloc(sizeof(link));
        curr = first->next;
    }
    else
    {
        if(line != NULL)
        {
            curr->word = malloc(100);
            strcpy(curr->word, line);
            curr->next = malloc(sizeof(link));
            curr = curr->next;
        }
    }
}

return first;
}

在你这么说之前——我知道我是 C 的初学者。非常感谢任何帮助,我是菜鸟 :)

4

2 回答 2

1

崩溃是因为您没有可靠地将next新分配的指针设置link为 NULL,但您的迭代取决于它是否为 NULL。在从中读取字符之前,您还需要检查它first->word是否为 NULL 。print_no_vowels()

此外,你不想要strlen(vowels)-1- 它离开了 y 的周围。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct linked_list_node link;

struct linked_list_node
{
    char *word;
    link *next;
};

static void print_no_vowels(link *lines)
{
    char vowels[] = "aeiouy";
    unsigned int i = 0;
    unsigned int j = 0;
    unsigned int check = 0;
    link *first = lines;

    while (first != NULL && first->word != NULL)
    {
        printf("%p: ", first);
        for (i = 0; i < strlen(first->word)-1; i++)
        {
            for (j = 0; j < strlen(vowels); j++)
            {
                if (first->word[i] == vowels[j])
                {
                    check = 1;
                }
            }
            if (check != 1)
            {
                printf("%c", first->word[i]);
            }
            else
            {
                check = 0;
            }
        }
        printf("\n");

        first = first->next;
    }
}

static link *read_file(FILE *input)
{
    char line[100];
    link *first = malloc(sizeof(link));
    first->word = NULL;
    first->next = NULL;
    link *curr = NULL;
    while (fgets(line, 100, input) != NULL)
    {
        if (first->word == NULL)
        {
            first->word = malloc(100);
            strcpy(first->word, line);
            first->next = malloc(sizeof(link));
            curr = first->next;
        }
        else
        {
            curr->word = malloc(100);
            strcpy(curr->word, line);
            curr->next = malloc(sizeof(link));
            curr = curr->next;
        }
        curr->next = NULL;
        curr->word = NULL;
    }

    return first;
}

int main(void)
{
    link *list = read_file(stdin);
    if (list != 0)
        print_no_vowels(list);
    return 0;
}

在其源代码上运行程序会产生:

0x7fe75b4000e0: #ncld <std.h>
0x7fe75b403a80: #ncld <stdlb.h>
0x7fe75b403b00: #ncld <strng.h>
0x7fe75b403b80: 
0x7fe75b403c00: tpdf strct lnkd_lst_nd lnk;
0x7fe75b403c80: 
0x7fe75b403d00: strct lnkd_lst_nd
0x7fe75b403d80: {
0x7fe75b403e00:     chr *wrd;
0x7fe75b403e80:     lnk *nxt;
0x7fe75b403f00: };
0x7fe75b403f80: 
0x7fe75b404000: sttc vd prnt_n_vwls(lnk *lns)
0x7fe75b404080: {
0x7fe75b404100:     chr vwls[] = "";
0x7fe75b404180:     nsgnd nt  = 0;
0x7fe75b404200:     nsgnd nt j = 0;
0x7fe75b404280:     nsgnd nt chck = 0;
0x7fe75b404300:     lnk *frst = lns;
0x7fe75b404380: 
0x7fe75b404400:     whl (frst != NULL && frst->wrd != NULL)
0x7fe75b404480:     {
0x7fe75b404500:         prntf("%p: ", frst);
0x7fe75b404580:         fr ( = 0;  < strln(frst->wrd)-1; ++)
0x7fe75b404600:         {
0x7fe75b404680:             fr (j = 0; j < strln(vwls); j++)
0x7fe75b404700:             {
0x7fe75b404780:                 f (frst->wrd[] == vwls[j])
0x7fe75b404800:                 {
0x7fe75b404880:                     chck = 1;
0x7fe75b404900:                 }
0x7fe75b404980:             }
0x7fe75b404a00:             f (chck != 1)
0x7fe75b404a80:             {
0x7fe75b404b00:                 prntf("%c", frst->wrd[]);
0x7fe75b404b80:             }
0x7fe75b404c00:             ls
0x7fe75b404c80:             {
0x7fe75b404d00:                 chck = 0;
0x7fe75b404d80:             }
0x7fe75b404e00:         }
0x7fe75b404e80:         prntf("\n");
0x7fe75b404f00: 
0x7fe75b404f80:         frst = frst->nxt;
0x7fe75b405000:     }
0x7fe75b405080: }
0x7fe75b405100: 
0x7fe75b405180: sttc lnk *rd_fl(FILE *npt)
0x7fe75b405200: {
0x7fe75b405280:     chr ln[100];
0x7fe75b405300:     lnk *frst = mllc(szf(lnk));
0x7fe75b405380:     frst->wrd = NULL;
0x7fe75b405400:     frst->nxt = NULL;
0x7fe75b405480:     lnk *crr = NULL;
0x7fe75b405500:     whl (fgts(ln, 100, npt) != NULL)
0x7fe75b405580:     {
0x7fe75b405600:         f (frst->wrd == NULL)
0x7fe75b405680:         {
0x7fe75b405700:             frst->wrd = mllc(100);
0x7fe75b405780:             strcp(frst->wrd, ln);
0x7fe75b405800:             frst->nxt = mllc(szf(lnk));
0x7fe75b405880:             crr = frst->nxt;
0x7fe75b405900:         }
0x7fe75b405980:         ls
0x7fe75b405a00:         {
0x7fe75b405a80:             crr->wrd = mllc(100);
0x7fe75b405b00:             strcp(crr->wrd, ln);
0x7fe75b405b80:             crr->nxt = mllc(szf(lnk));
0x7fe75b405c00:             crr = crr->nxt;
0x7fe75b405c80:         }
0x7fe75b405d00:         crr->nxt = NULL;
0x7fe75b405d80:         crr->wrd = NULL;
0x7fe75b405e00:     }
0x7fe75b405e80: 
0x7fe75b405f00:     rtrn frst;
0x7fe75b405f80: }
0x7fe75b406000: 
0x7fe75b406080: nt mn(vd)
0x7fe75b406100: {
0x7fe75b406180:     lnk *lst = rd_fl(stdn);
0x7fe75b406200:     f (lst != 0)
0x7fe75b406280:         prnt_n_vwls(lst);
0x7fe75b406300:     rtrn 0;
0x7fe75b406380: }

代码还有很大的改进空间,但至少没有崩溃。除其他事项外,您需要编写代码释放列表。它仍然应该被修改使用strdup()

于 2013-09-14T20:33:11.207 回答
0
for(i = 0; i < strlen(first->word)-1; i++)
    {
        for(j = 0; j < strlen(vowels)-1; j++)

您不想使用 strlen()-1,而只是使用 strlen。空行中的空字符串将 i 下溢到 0xFFFFFFFF(如果您在 32 位架构上运行)并导致您访问 word 中的无效内存

编辑:您还需要将列表的最后一个元素初始化为 NULL

 curr = first->next;
 curr->next = NULL;

curr = curr->next;
curr->next = NULL;
于 2013-09-14T20:12:10.503 回答