468,513 Members | 1,686 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Dynamic array of structures within a structure

I have a problem regarding array of structures within a structure. I have an static array of structure within a structure eg.

typedef struct
{
int x;
}A;

typedef struct
{
int no_of_a;
A a[5];
int y;
int z;
}B;

Where no_of_a gives the number of values of structure A I have to store in B

Now my problem is that since this is static therefore even if "no_of_a" = 1, my application shall allocate memory for 5 elements of A in structure B.

How can I define the structure B such that depending on the value of "no_of_a", my application shall allocate memory for as many elements of A in structure B

I would appreciate any help.
Aug 30 '06 #1
2 5997
rgb
37
one solution might be: you can encapsulate the variables in a class and create a constructor that accepts the "no_of_a" value and initializes the A[ ] array based on the "no_of_a" required.
Aug 30 '06 #2
Banfa
9,056 Expert Mod 8TB
In B just declare a pointer to structure of type A and then when you know the value of no_of_a allocate the required data using malloc or new.
Aug 30 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Peter B. Steiger | last post: by
12 posts views Thread by shailashri_sk | last post: by
5 posts views Thread by Joe Thompson | last post: by
104 posts views Thread by Leszek | last post: by
12 posts views Thread by googlinggoogler | last post: by
9 posts views Thread by Spoon | last post: by
1 post views Thread by Gurur | last post: by
5 posts views Thread by =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.