Struct inside a struct

Discussion in 'C++' started by monkeydragon, Dec 18, 2005.

  1. monkeydragon

    monkeydragon Guest

    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
     
    monkeydragon, Dec 18, 2005
    #1
    1. Advertising

  2. monkeydragon

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Dec 18, 2005
    #2
    1. Advertising

  3. monkeydragon

    monkeydragon Guest

    "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
     
    monkeydragon, Dec 19, 2005
    #3
  4. monkeydragon

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Dec 20, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark McIntyre

    C beginner: struct inside another struct...

    Mark McIntyre, Apr 21, 2006, in forum: C Programming
    Replies:
    5
    Views:
    670
    Yourko
    Apr 23, 2006
  2. Replies:
    3
    Views:
    562
  3. arnuld

    struct inside struct

    arnuld, Jul 23, 2011, in forum: C Programming
    Replies:
    5
    Views:
    739
    Kleuskes & Moos
    Jul 24, 2011
  4. none
    Replies:
    8
    Views:
    402
  5. Richard Mathis

    Issues with declaring struct arrays inside of a struct

    Richard Mathis, Oct 10, 2003, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    241
    Richard Mathis
    Oct 10, 2003
Loading...

Share This Page