源代码如下
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct station{
char name[30];
int is_stop_limited;
int is_stop_express;
int is_stop_local;
struct station *next_station;
} station;
void make_list(station *list, station *p){
scanf("%s %d %d %d", list->name, &list->is_stop_limited, &list->is_stop_express, &list->is_stop_local);
list->next_station = NULL;
while(1){
char name[30];
int num1, num2, num3;
scanf("%s %d %d %d", name, &num1, &num2, &num3);
if(strcmp(name, "none")== 0){
break;
}else{
p = (station *)malloc(sizeof(station));
p->is_stop_limited = num1;
p->is_stop_express = num2;
p->is_stop_local = num3;
strcpy(p->name, name);
p->next_station = NULL;
if(p == NULL){
printf("error");
exit(1);
}
for(list; list->next_station != NULL; list = list->next_station){
}
list->next_station = p;
}
}
}
void put_station(struct station *stations){
for(stations; stations != NULL; stations = stations->next_station){
printf("%s ", stations->name);
}
printf("\n");
}
int main(){
struct station *stations;
struct station *ptr;
struct station *begin = stations;
make_list(stations, ptr);
put_station(begin);
free(ptr);
return 0;
}
在执行后输入第二串数据时make_list最后的for报出了Segmentation fault的错误,虽然感觉是出现了野指针但是不知道具体的位置,求指点