Hi, I am using VC++ and I need to know how to make a pointer to a variable size array in a structure. For example say this:





struct EGGHEAD {

EGG *eggsinhead[ ]; ;Is this how I would do it?



BTW Egg is another structure

What I want to do is have a malloc call in the runtime and then write the pointer into the structure and then access them as an array of structures, using the "->" operator, but MSVC++ gives me this error when I do the above:



warning C4200: nonstandard extension used : zero-sized array in struct/union


HELP !! PLEASE?!?! All my C books dont tell me how to do this, but I know it can be done!
Posted on 2003-04-03 07:29:51 by x86asm
It isn't an error, its a warning. Nonetheless, your code might better be standard, so just use EGG **eggsinhead. In C(++), the name of an array is the same as a pointer to its first element. So the double pointer can be used in the same way as the array.

Thomas

edit:
What I want to do is have a malloc call in the runtime and then write the pointer into the structure and then access them as an array of structures, using the "->" operator

Then your code nor mine will do what you want, if you just want to create a dynamic array of EGG structs, use EGG *eggsinhead.
Posted on 2003-04-03 08:34:12 by Thomas
ITs weird its confusing, I think I should clarify :)
I did what you said but I believe I should ask some more questions, anyway
I noticed there is a keyword called "new", does this keyword allocate memory?
Also I would like to have a pointer to an array of arrays :)

Let me use a picture :)

->ARRAY["I want this to be variable size"], I did what you did Thomas but MSVC++ is complaining again, and its quite frustrating!! >=|
Posted on 2003-04-03 19:50:22 by x86asm
"EGG **eggsinhead" points to a pointer to an EGG. You can allocate a number of those pointers using:

EGG MasterEgg;
MasterEgg.eggsinhead = new EGG *

where N is the number of pointers to allocate. The pointers can then be used like this:

MasterEgg.eggsinhead[5] = new EGG;
MasterEgg.eggsinhead[5]->eggsinhead = new EGG *

This actually allocates another array (which turns this into a tree, yay!).

Remember to do "delete [] MasterEgg.eggsinhead[5]->eggsinhead", "delete MasterEgg.eggsinhead[5]" and then "delete [] MasterEgg.eggsinhead".
Posted on 2003-04-04 00:21:55 by gliptic
Thanks I fixed it, much thanks to you gliptic and Thomas :alright:
Posted on 2003-04-04 07:09:27 by x86asm
The real C++ way to go is using a vector:


vector<EGG> eggVector;


Or a dynamic array of arrays:
vector< vector<EGG> > eggVector;

(though you might want to use a single dimension vector instead for efficiency).

Thomas
Posted on 2003-04-04 13:15:38 by Thomas