C中的反问题和分段故障(核心转储)

时间:2014-11-22 00:03:55

标签: c linked-list counter

我目前正在使用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.\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)

2 个答案:

答案 0 :(得分:2)

想想这段代码:

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

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

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

答案 1 :(得分:0)

char maininput[1];

和这个

scanf("%1s", maininput);

彼此不能很好地合作。

%s是以\ 0结尾的字符数,因此当您编写%1s时,您至少需要2个字符。

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