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



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

但老实说,我希望能够解决这个问题,同时也实现如果用户输入不在链表中的电影标题,它会打印出来:"电影在数据库中不存在。

此外,当我将新电影更新到链表中时,计数器也不会增加。它一直保持在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.nn", 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 = first;
while(current != NULL) { 
    if (strcmp(searchinput, current->title) == 0)
        show_structure(current);
    current=current->next;
}

这个

char maininput[1];

而这个

scanf("%1s", maininput);

彼此合作不好。

%s 是以 \0 结尾的字符数,因此当您写入 %1 时,您期望至少包含 2 个字符。

而是使用 fgetc(stdin) 从控制台读取一个字符。

相关内容

  • 没有找到相关文章

最新更新