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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:00:41+00:00 2026-06-13T13:00:41+00:00

Hi there I am having a bit of trouble with defining an array of

  • 0

Hi there I am having a bit of trouble with defining an array of structures within a structure.
This is my idea, I need to have a structure called figure which holds the name of the figure, coordinate count and the coordinates (x,y). Each figure can have an arbitrary amount of coordinates.
I also need to be able to dynamically reallocate space for an ever increasing list of coords… Please help point me in the right direction.
thank you,

tyler

typedef struct {
  char fig_name[FIGURE_LEN + 1];
  int coordcount;
  /* here i need to declare an array of coord structures that 
     but i am not sure how to do this properly. I was originally
     going to try something like as follows */
  coords *pointer;
  pointer = malloc(sizeof(coords));
  pointer = coords figcoord[];
  /* however i am quite certain that this would not work */
} figure;

typedef struct {
  double x;
  double y;
} coords;
  • 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-13T13:00:42+00:00Added an answer on June 13, 2026 at 1:00 pm

    Kick toward the right direction. Try something like this. I apologize for the lack of error checking for the malloc() calls, but you will get the general idea (I hope):

    #include <stdlib.h>
    
    #define FIGURE_LEN  128
    
    typedef struct
    {
        double x;
        double y;
    } coords;
    
    typedef struct
    {
        char fig_name[FIGURE_LEN + 1];
        int coordcount;
        coords *pointer;
    } figure;
    
    
    /* allocate a dynamic allocated figure */
    figure* alloc_figure(char* name, int coordcount)
    {
        figure *fig = malloc(sizeof(figure));
        fig->coordcount = coordcount;
        fig->pointer = malloc(sizeof(coords) * coordcount);
        strncpy(fig->fig_name, name, FIGURE_LEN);
        fig->fig_name[FIGURE_LEN] = 0;
        return fig;
    }
    
    /* release a dynamic allocated figure */
    void free_figure(figure** ppfig)
    {
        if (!*ppfig)
            return;
    
        free((*ppfig)->pointer);
        free(*ppfig);
        *ppfig = NULL;
    }
    
    int main(int argc, char *argv[])
    {
        figure fig;
        fig.coordcount = 10;
        fig.pointer = malloc(10 * sizeof(coords));
    
        /* access fid.pointer[0..9] here... */
        fig.pointer[0].x = 1.0;
        fig.pointer[0].y = 1.0;
    
        /* don't  forget to free it when done */
        free(fig.pointer);
    
        /* dynamic allocation function use */
        figure *fig1 = alloc_figure("fig1", 10);
        figure *fig2 = alloc_figure("fig2", 5);
    
        fig1->pointer[9].x = 100.00;
        fig2->pointer[0].y = fig1->pointer[9].x;
    
        /* and use custom free function for releasing them */
        free_figure(&fig1);
        free_figure(&fig2);
    
        return EXIT_SUCCESS;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a bit of trouble getting this right... The main objective is to have
I'm having a bit of trouble with dynamic_casting. I need to determine at runtime
I'm having a bit of trouble with this one, as basic as it may
I am having a bit of trouble figuring out why there are missing items
I am having a bit of trouble navigating around an NSArray . My array:
I'm having a bit of trouble calling the incrementer i in this loop in
Having a bit of trouble with htaccess, at the moment I have a rewrite
I'm having a bit of trouble deserializing an XML that doesn't have a namespace.
I'm having a bit of trouble trying to get this quicksort algorithm to work
I'm having a bit of trouble trying to figure out the best way to

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.