C在使用内核链表时遇到问题
我正在尝试在我的航班信息系统程序中使用此文件 但是,当我试图将新节点链接到链表的第一个节点时,我的程序似乎遇到了一些问题 这是我的密码C在使用内核链表时遇到问题,c,linked-list,C,Linked List,我正在尝试在我的航班信息系统程序中使用此文件 但是,当我试图将新节点链接到链表的第一个节点时,我的程序似乎遇到了一些问题 这是我的密码 #include somefiles balabala typedef struct flight { char ID[10]; char departure[20]; char arrival[20]; char dep_date[8]; char dep_time[4];
#include somefiles balabala
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)); // Seems the problem will have a **segment fault** here
}
int main (void) {
p_flight list = init_list();
add_flight (list);
return 0;
}
我不确定这是什么原因造成的。
是我访问内存新建节点->列表导致了这一点还是其他原因
谢谢大家!
scanf(“%f”,新节点->信息价格)代码>调用未定义的行为来传递类型不正确的数据,并且在典型环境中很有可能崩溃。@MikeCAT新节点->info.price不是地址吗?我很困惑,当我传递像char-type这样的值时,它是一个地址,而不是float?如果我像这样定义price
,当我使用scanf(“%f”,new\u node->info.price)
,尝试传递数据时,它能工作吗?new\u node->info.price
有类型float
,它肯定不是地址。char
肯定不是地址,如果您在调用scanf(“%f”,new\u node->info.price)
之前分配足够的缓冲区并将其地址存储到new\u node->info.price
,则更改为float*price应该可以工作。