446,402 Members | 936 Online 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; upper[x].lower* plates = new upper[x].lower; how can i access each element in this way.. table.plates .width_row = 10; // for example table.plates .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
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; upper[x].lower* plates = new upper[x].lower; upper::lower* plates = new upper::lower; how can i access each element in this way.. table.plates .width_row = 10; // for example table.plates .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; table.plates = new upper::lower; table.plates.width_row = 10; delete[] table.plates; delete[] table; } However, I suggest using std::vector, which is easier to use than a dynamic array, but gives the same functionality: #include struct upper { struct lower { BYTE* row; UINT width_row; }; std::vector plates; }; int main() { std::vector table; table.resize(100); table.plates.resize(100); table.plates.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. 