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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:00:26+00:00 2026-06-15T12:00:26+00:00

I have a struct called course and each course has multiple nodes (another struct

  • 0

I have a struct called course and each course has multiple nodes (another struct ‘node’).

The number of nodes it has varies but I am given that number from a file that I am reading this information from, so that number sits in a variable.

So I need a malloc inside the struct. But I am confused. I know you can have arrays in structs but I don’t know where to put the code that creates the malloc array since my struct is in my header file. Here’s my code at the moment. I realize it looks wrong, I just don’t know how I can fix it and where to initialize the malloc array.

struct course {
    char identifier[2];
    int num_nodes;
    struct node *nodes;
    nodes = (struct nodes*)malloc(num_nodes*sizeof(struct node));
};

struct node {
    int number;
    char type[2];
};

I want to be able to do something like:

struct node a_node;
struct course a_course;

a_course.nodes[0] = a_node;

etc…

I haven’t used much C, this is the first time I’ve ever tried using dynamic arrays in C. My experience all comes from Java, and of course Java doesn’t really use pointers in the same way as C so it’s all a tad confusing for me.

So some help would be much appreciated, thanks a lot 🙂

  • 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-15T12:00:27+00:00Added an answer on June 15, 2026 at 12:00 pm

    The easiest approach is to create a function which initialises the struct:

    void init_course(struct course* c, const char* id, int num_nodes)
    {
        strncpy(c->identifier, id, sizeof(c->identifier));
        c->num_nodes = num_nodes;
        c->nodes = calloc(num_nodes, sizeof(struct node));
    }
    

    For symmetry, you could also then define a destructor

    void destroy_course(struct course* c)
    {
        free(c->nodes);
    }
    

    These would have usage like

    struct course c;
    init_course(&c, "AA", 5);
    /* do stuff with c */
    destroy_course(&c);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a struct that has a field called type How do i access
I have a struct called coordinate which is contained in a list in another
I have a struct array called AnalysisResults , that may contain any MATLAB datatypes,
Suppose I have a struct called foo_boolean that contains some boolean values: struct foo_boolean
Let's say I have a struct called Woah . Woah has one field to
I have two classes: MazeClass and CreatureClass, that use a struct called "coordinates," how
I have a struct called node as follows: struct node { int data; }
I have a 32x1 struct array . Each element of this array has several
Suppose I have a struct called Node as follows: struct foo { foo *next;
I have a struct called Point. Point is pretty simple: struct Point { Row

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.