By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,402 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,402 IT Pros & Developers. It's quick & easy.

Struct inside a struct

P: n/a
hi,
i am making a struct within a struct like this..

struct upper
{
struct lower{
BYTE* row;
UINT width_row;
}
}

upper* table= new upper[100];
upper[x].lower* plates = new upper[x].lower[100];

how can i access each element in this way..
table[0].plates [0].width_row = 10; // for example
table[0].plates [1].width_row = 12;
table[n].plates [m].width_row = something;
.... so on

so that later i can do:

table[n].plates[m].row = new BYTE[table[n].plates [m].width_row];

Thx

Dec 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
monkeydragon wrote:
hi,
i am making a struct within a struct like this..

struct upper
{
struct lower{
BYTE* row;
UINT width_row;
}
;
}
;

upper* table= new upper[100];
upper[x].lower* plates = new upper[x].lower[100];
upper::lower* plates = new upper::lower[100];
how can i access each element in this way..
table[0].plates [0].width_row = 10; // for example
table[0].plates [1].width_row = 12;
table[n].plates [m].width_row = something;
... so on
In this case, your upper class needs to contain an ary of lower, or - if you
need a dynamic size, a pointer to lower, like:

struct upper
{
struct lower
{
BYTE* row;
UINT width_row;
};

lower* plates;
};

Then you can do something like:

int main()
{
upper* table = new upper[100];
table[0].plates = new upper::lower[100];

table[0].plates[0].width_row = 10;

delete[] table[0].plates;
delete[] table;
}
However, I suggest using std::vector, which is easier to use than a dynamic
array, but gives the same functionality:

#include <vector>

struct upper
{
struct lower
{
BYTE* row;
UINT width_row;
};

std::vector<lower> plates;
};

int main()
{
std::vector<upper> table;
table.resize(100);
table[0].plates.resize(100);
table[0].plates[0].width_row = 10;
}

Note that there is no need for deleting the memory. That is done
automatically.
so that later i can do:

table[n].plates[m].row = new BYTE[table[n].plates [m].width_row];


Again, use a vector for that instead of raw arrays. It will also keep track
of its size.
Dec 18 '05 #2

P: n/a
"upper" get its value change every loop..
i am wondering if when you we use table.resize
say from table.resize(10) then table.resize(20);
what will happen to data from 0-9? will it be still there?

i.e.
count = 1
do{
table.plates.resize(count)
// some routine...
++count;
}while(SOME_CONDITION);

krby_xtrm

Dec 20 '05 #3

P: n/a
monkeydragon wrote:
"upper" get its value change every loop..
i am wondering if when you we use table.resize
say from table.resize(10) then table.resize(20);
what will happen to data from 0-9? will it be still there?
Yes. However, any pointers or iterators to the data may be invalidated.
i.e.
count = 1
do{
table.plates.resize(count)
// some routine...
++count;
}while(SOME_CONDITION);


If you want to add a value at every iteration, I'd recommend to use
push_back() instead, like:

do{
// get new value
table.plates.push_back(new_value);
}while(SOME_CONDITION);

push_back will automatically resize the vector.
Dec 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.