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

  • Home
  • SEARCH
  • 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 8775105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:43:32+00:00 2026-06-13T18:43:32+00:00

I am trying to have dynamically allocate arrays of structures and perform operations on

  • 0

I am trying to have dynamically allocate arrays of structures and perform operations on them but i keep running into segmentation faults. could someone help me out?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void *malloc(size_t size);

typedef struct {
  double x;
  double y;
} coords;

struct figure {
  char fig_name[128];
  int coordcount, size_tracker;
  coords *pointer;
} fig;

void init_fig(int n, struct figure **point)
{
  printf("%u\n", sizeof(coords));
  point[n]->pointer = malloc(sizeof(coords) * 20);  <-------SEGFAULT
  if (point[n]->pointer == NULL){
    exit(-1);
  }
  point[n]->pointer[19].x = 2;
  point[n]->pointer[0].x = 1;
  point[n]->pointer[0].y = 2;
  point[n]->pointer[7].x = 100;
}

int main()
{
  int numfigs = 1;
  struct figure * point;
  point = malloc(sizeof(struct figure) * 16);
  point = &fig;
  point[1].coordcount = 1;
  init_fig(numfigs, &point);
  return 0;
}

I labelled where the first seg fault occurs, (used ddd). what i dont get is that i can manipulate point[1] in main but not in any other function.

  • 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-13T18:43:33+00:00Added an answer on June 13, 2026 at 6:43 pm

    I agree with @Maxim Skurydin.
    Nevertheless I’d like to explain your mistake in some more details.

    Reading your init_fig one assumes that the parameter you pass struct figure **point – is actually array of pointers to struct figure. And this function accesses its n‘th element.

    However in your main you do something else. You allocate an array of struct figure, and your point variable points to its head. Then you take the address of this local variable and call your init_fig.

    Here’s the problem. init_fig assumes that you pass it an array of pointers, whereas actually this “array” consists of a single element only: the local point variable declared in main.

    EDIT:

    How to do this properly.

    1. Leave main intact, fix init_fig.

    This means that actually there’s an array of figure structs. Means – a single memory block, interpreted as an array of consequent structs.

    void init_fig(int n, struct figure *point)
    {
      printf("%u\n", sizeof(coords));
      point[n].pointer = malloc(sizeof(coords) * 20);  <-------SEGFAULT
      if (point[n].pointer == NULL){
        exit(-1);
      }
      point[n].pointer[19].x = 2;
      point[n].pointer[0].x = 1;
      point[n].pointer[0].y = 2;
      point[n].pointer[7].x = 100;
    }
    
    1. Leave init_fig intact. Fix main.

    This means that we actually should allocate an array of pointers, every such a pointer should point to an allocated point structure.

    int main()
    {
      int numfigs = 1;
      struct figure ** point;
      point = malloc(sizeof(struct figure*) * 16);
    
      for (i = 0; i < 16; i++)
        point[i] = malloc(sizeof(struct figure));
    
      point[1].coordcount = 1;
      init_fig(numfigs, &point);
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection of arrays which are generated dynamically. What I am trying
I am trying to have my interface dynamically generate a customized button when I
I have a method which I am trying to call dynamically. That method has
I have a python script that is trying to create a directory tree dynamically
I am trying to dynamically make divs that are clickable. I have inserted a
I have a javascript question. I'm trying to dynamically build a plugins string variable
In C#.net, I have the following DataSource setup that I am trying to dynamically
I have to learn memory management in class and how to dynamically allocate memory
I am trying to dynamically have my javascript look for an element ID in
I'm trying to set the src of an img tag I have dynamically. The

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.