Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:48:16+00:00 2026-06-05T06:48:16+00:00

So, I am trying to make a map for my class, I was practicing

  • 0

So, I am trying to make a map for my class, I was practicing myself try to implement a nested loop to see if I take a “NewYork—-250km——LosAngeles” kinda road, I should be able to give the NewYork as a previous city name and LosAngeles as a next city name. Distance is 250 km. I am taking the memory for the city name, road and city but after I type from the keyboard for the “next_city” part, I am getting segfault. Can someone please help me out what am I doing wrong?

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..

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:48:17+00:00Added an answer on June 5, 2026 at 6:48 am

    That’s because ROAD->next_city doesn’t point to a valid address, it’s a dangling pointer.

    Try the following:

    road *ROAD = malloc(sizeof(road));
    city *next_city = malloc(sizeof(city));
    ROAD->next_city = next_city; 
    

    Is this what you originally intended to do?

    Also, note that you should free the memory you acquire via malloc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a class that takes a map as a template parameter.
I am trying to make a map where a user can outline any shape
I'm trying to make a type with an optional map: module CharMap = Map.Make(Char)
I am trying to make a temperature map, all works fine but I don't
I'm trying to make a sample map app, but the map doesn't appear on
i'm trying to make a site map and i have a question: my htaccess
I am trying to make a terrain using a height map image in OpenGL.
I'm trying to make a table starting from a map with a list as
i'm trying to make tower defense game, i'm implementing game map for fast lookup
I have been trying to make a StringTable class that holds a simple unordered_map<string,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.