首页 > C语言 在使用内核链表的时候遇到了段错误

C语言 在使用内核链表的时候遇到了段错误

在做一个航班管理系统练习的时候使用了内核链表写了以下代码

// 省略一些头文件包含

typedef struct flight {
        char ID[10];
        char departure[20];
        char arrival[20];
        char dep_date[8];
        char dep_time[4];
        char arr_time[4];
        float price;
        struct list_head list;
} flight;

typedef struct node {
        flight info;
        struct list_head list;
} flight_node, *p_flight;

p_flight init_list () {
        p_flight head = malloc (sizeof (flight_node));
        INIT_LIST_HEAD (&(head->list));
        return head;
}

void add_flight (p_flight node) {
        p_flight new_node;
        new_node = malloc (sizeof(flight_node));

        system ("clear");
        printf (" \e[1;36m>\33[0m Importing new flight info:\n");
        printf (" Please input the \e[1;33mID\33[0m of the filght: ");
        scanf ("%s", new_node->info.ID);
        printf (" Please input the \e[1;33mdeparture\33[0m of the filght: ");
        scanf ("%s", new_node->info.departure);
        printf (" Please input the \e[1;33marrival\33[0m of the filght: ");
        scanf ("%s", new_node->info.arrival);
        printf (" Please input the \e[1;33mdeparture date\33[0m of the filght: ");
        scanf ("%s", new_node->info.dep_date);
        printf (" Please input the \e[1;33mdeparture time\33[0m of the filght: ");
        scanf ("%s", new_node->info.dep_time);
        printf (" Please input the \e[1;33marrival time\33[0m of the filght: ");
        scanf ("%s", new_node->info.arr_time);
        printf (" Please input the \e[1;33mprice\33[0m of the filght: ");
        scanf ("%f", new_node->info.price);
        list_add (&(new_node->list), &(node->list));    //执行到这里会遇到段错误
}

int main (int argc, char *argv[]) {
        printf ("\n\n\n");
        p_flight list = init_list();
        add_flight (list);
        return 0;
}

我使用的是一个从linux内核源码独立出来的内核链表.h文件

add_flight函数里,当我尝试将新节点链接到已经初始化的链表头的时候遇到了段错误


        printf (" Please input the \e[1;33mprice\33[0m of the filght: ");
        scanf ("%f", new_node->info.price); // !! &new_node->info.price
【热门文章】
【热门文章】