崩溃是因为您没有可靠地将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()
。