gpt4 book ai didi

C 中的计数器问题和段错误(核心转储)

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

我目前正在用 C 进行编码,并且在尝试选择性地搜索电影信息/评级结构的链接列表时遇到困难。到目前为止,我刚刚获得了段错误核心转储。我当前的算法基本上 while 循环 current = current->next 除非它在链表中达到 NULL。如果用户输入与链接列表中的标题信息匹配,它将打印出电影详细信息(通过 show_struct())。

但说实话,我希望能够解决这个问题,同时还实现如果用户输入不在链接列表中的电影标题,它将打印出:“数据库中不存在电影。 ”

此外,当我将新电影更新到链接列表中时,计数器也不会增加。它一直停留在 1。有什么想法吗?

这是我到目前为止的代码:

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

struct movie_node {
char title[250];
int year;
unsigned char rating;
struct movie_node *next;
};

typedef struct movie_node movie_t;

movie_t *first;
movie_t *current;
movie_t *new;
movie_t *make_structure(void);

void fill_structure(movie_t *a);
void show_structure(movie_t *a);


int main()
{
int counter = 0;
char traverse = 1;
char maininput[1];
char searchinput[250];
while (traverse)
{
printf("%d Movie(s) in the database. Update, Search, or Quit (U/S/Q): ", counter);
scanf("%1s", maininput);

if (strcmp("U", maininput) == 0)
{
if (counter == 0)
{
first = make_structure();
current = first;
}
else
{
new = make_structure();
current->next=new;
current=new;
}
counter++; //COUNTER DOESN'T INCREMENT
fill_structure(current);
current->next = NULL;
printf("Movie %s is added to the database.\n\n", current->title);
}
if (strcmp("S", maininput) == 0)
{

printf("Name of the movie: ");
scanf(" %[^\n]%*c", searchinput);
current = first;

do
{
current=current->next;
if (strcmp(searchinput, current->title) == 0) //PROBLEM LIES HERE
{show_structure(current);}
} while(current != NULL);
}
if (strcmp("Q", maininput) == 0)
{
traverse = 0;
}
}
return 0;
}

movie_t *make_structure(void)
{
movie_t *a;
a = (movie_t *)malloc(sizeof(movie_t));
return a;
}

void fill_structure(movie_t *a)
{
printf("Name of the movie: ");
scanf(" %[^\n]%*c", a->title);
printf("Year: ");
scanf("%d", &a->year);
printf("Rating: ");
scanf("%hhu", &a->rating);
}

void show_structure(movie_t *a)
{
printf("Year: %d ", a->year);
printf("Rating: %hhu", a->rating);
}

现在的输出是这样的:

0 Movie(s) in the database. Update, Search, or Quit (U/S/Q): U
Name of the movie: Pulp Fiction
Year: 1994
Rating: 5
Movie Pulp Fiction is added to the database.

1 Movie(s) in the database. Update, Search, or Quit (U/S/Q): U
Name of the movie: Forrest Gump
Year: 1994
Rating: 5
Movie Forrest Gump is added to the database.

1 Movie(s) in the database. Update, Search, or Quit (U/S/Q): S
Name of the movie: Pulp Fiction
Segmentation fault (core dumped)

最佳答案

考虑一下这段代码:

do
{
current=current->next;
if (strcmp(searchinput, current->title) == 0) //PROBLEM LIES HERE
{show_structure(current);}
} while(current != NULL);

当 current 是最后一个元素时,它不为 null。你进入循环的顶部,将current设置为current->next,即null,然后尝试访问current->title。但当前为NULL。这是一个问题。这样会更好:

current = first;
while(current != NULL) {
if (strcmp(searchinput, current->title) == 0)
show_structure(current);
current=current->next;
}

关于C 中的计数器问题和段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27072433/

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