Hi, I'm a novice when it comes to programming. Right now I'm in college taking my first class on C++, but our first homework assignment is in C.
Anyways, the first programming assignment is fairly large and I've been working on it for a few days now.
However, there's a certain part of the program where I'd love to utilize structures and dynamically allocated arrays for a specific purpose.
I created a test program in C to kinda test out what I want to do...but I can't get it to compile. (If it doesn't compile, then surely it won't work in the grand scheme of things.)
I would be grateful if anyone can point me in the right direction and help me find a solution for what I want to accomplish.
Basically I'm trying to create a structure that contains a dynamically allocated array of structures, with each of these structures containing a dynamically allocated array of integers. So far I've been unsuccessful in getting something like this to compile (I use g++ if that matters)...
I've fiddled around a bit...but I can't get it to compile. What am I doing wrong? Is there some kind of looping involved?Code:#include <stdio.h> #include <stdlib.h> // I want to create a "parent structure" // that is capable of holding a dynamically allocated // array of "child structures" // // each of these "child structures" would then hold // a dynamically allocated array of integers.. // "child structure" typedef struct { int *numbers; // dynamically allocated array of ints } child_structure; // "parent structure" typedef struct { child_structure *items; // dynamically allocated array of child stucts } parent_structure; int main(void) { parent_structure test; int N, n; // first i dynamically allocate the array of child structures... test.items = (child_structure *)malloc(sizeof(child_structure) *N); // then i dynamically allocate the array of integers in each of child structures // ... at least that's what i'm trying to do! test.items.numbers = (int *)malloc(sizeof(int) *n); return (0); }
Thanks in advance for the help.