所以,我正在尝试为我的班级制作地图,我正在练习自己尝试实现一个嵌套循环,看看我是否走“NewYork----250km-----LosAngeles”这条路,我应该是能够将 NewYork 作为前一个城市名称,将 LosAngeles 作为下一个城市名称。距离为 250 公里。我正在记忆城市名称、道路和城市,但是在我从键盘输入“next_city”部分后,我遇到了段错误。有人可以帮我解决我做错了什么吗?
typedef struct road road;
typedef struct city city;
struct city{
int visited;
int distance;
int path;
char *city_name;
};
struct road{
int km;
struct city *next_city, *previous_city;
};
int main()
{
char *a=malloc(sizeof(char)*10);
char *b=malloc(sizeof(char)*10);
city *NewYork = malloc(sizeof(city));
NewYork->city_name = fgets(a,10,stdin); //this gives no error
road *ROAD = malloc(sizeof(road));
city *next_city = malloc(sizeof(city)); //to see if I can get a memory for LosAngeles
ROAD->next_city->city_name = fgets(b,10,stdin); //but here it gives a segfault after I type the name to terminal..
}