我正在开发一个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("%pn", 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;
}
}
在大量打印之后,我得出的结论是,程序再次进入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的初学者。非常感谢任何帮助,我是一个菜鸟:)
崩溃是因为您没有可靠地将新分配的link
的next
指针设置为 NULL,但您的迭代取决于它是 NULL。 在从中读取字符之前,您还需要检查first->word
是否print_no_vowels()
为 NULL。
另外,你不想要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()
。
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;