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 8777311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:15:00+00:00 2026-06-13T19:15:00+00:00

typedef struct { double x; double y; long out_x; long out_y; } coords; typedef

  • 0
typedef struct {
  double x;
  double y;
  long out_x;
  long out_y;
} coords;

typedef struct {
  char name[FIGURE_LEN + 1];
  int coordcount, size_tracker;
  coords *coordinate;
} fig;

fig figure;
fig * figpoint;

this is the functions that are being called from the parser.c source file.

void initialize_fig(int n, char * name, double x, double y,
                         fig *fig_point)
{
  printf("inside function\n");
  strncpy(fig_point[n].name, name, FIGURE_LEN + 1);
  fig_point[n].size_tracker = 1;
  fig_point[n].coordinate = malloc(sizeof(coords) * fig_point[n].size_tracker);
  assert(fig_point[n].coordinate != NULL);
  fig_point[n].coordcount = 0;
  fig_point[n].coordinate[fig_point[n].coordcount].x = x;
  fig_point[n].coordinate[fig_point[n].coordcount].y = y;
}

void create_FigArray(fig * fig_point, int fig_size)
{
  fig_point = malloc(sizeof(fig) * fig_size);
  assert(fig_point != NULL);
  fig_point = &figure
}

i first call create_FigArray like so…

create_FigArray(fig_point, 16);

i get no seg fault here…
but then i call…

initialize_fig(0, Qe, 10.0000, 10.0000, fig_point);

the parameters are actually passed through variables but i just wanted to show that they are proper parameters and give an example of what values are passed.
Anyways it hits

strncpy(fig_point[n].name, name, FIGURE_LEN + 1);

and stops.. segfault must happen here, but why?!

please help, explain and show how to get around this. thank you.

  • 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-13T19:15:01+00:00Added an answer on June 13, 2026 at 7:15 pm

    You allocate memory, then you change the pointer

    fig_point = malloc(sizeof(fig) * fig_size); // allocate here
    assert(fig_point != NULL);
    fig_point = &figure;  // now you make fig_point point to something else
    

    therefore your fig_point pointer no longer points to your dynamically allocated memory. If you then do this

    fig_point[n]
    

    you are accessing memory out or range since figure is not an array. In addition you pass the pointer fig_point to create_FigArray directly. This will create a copy of the pointer and hence any changes you make to that argument are in fact only changes to the copy. This means that the dynamic memory you that the address stored in fig_array after create_FigArray has returned is the same as it was before – it is only the copy that was changed by the function. If you want to change the pointer you need to use a double pointer argument to the function and then something like

    void create_FigArray(fig** fig_point, int fig_size)
    {
        *fig_point = malloc(sizeof(fig) * fig_size);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my struct typedef struct { char mmsi[10]; char name[20]; double latitude; double
I have a structure typedef struct store { char name[11]; int age; } store;
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
I have a struct typedef struct { int id; string name; string address; string
I have a typedef boost::variant<int, float, double, long, bool, std::string, boost::posix_time::ptime> variant which I
I have the following variant from the boost lib: typedef boost::variant<int, float, double, long,
I have the following struct: typedef struct{ int vin; char* make; char* model; int
I have a question about this code: typedef struct pop { unsigned long int
typedef struct { double x, y; } vec; typedef struct { int n; vec*
For example in C I have structure: typedef struct { int number; double x1;

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.