0

Hello there. I recently have been teaching myself C++ and have come across something that I just can't seem to grasp. I undersatdn the basics of structures but when it comes to this nesting type situation I just can't figure out what is going on. If someone could explain in a somewhat simple manner it would help me greatly

Here's the code:

#include <iostream>

using namespace std;



  struct sct {
        int t[2];
    };

    struct str {
        sct t[2];
    };

int main(void) {
   str t[2] = { {0, 2, 4, 6}, {1, 3, 5, 7} };
            cout << t[1].t[0].t[1] << t[0].t[1].t[0];
    return 0;
}

The output is 34 but I honestly can't figure out why! Thanks in advance any help would be greatly appreciated!

2
Contributors
2
Replies
38
Views
3 Years
Discussion Span
Last Post by mpike
1

This might make you better understand:

#include <iostream>

using namespace std;



struct sct {
    int t1[2];
};

struct str {
    sct t2[2];
};

int main(void) {
   str t3[2] = { {0, 2, 4, 6}, {1, 3, 5, 7} };
   cout << t3[1].t2[0].t1[1] << t3[0].t2[1].t1[0];
   return 0;
}


And the crude diagram of your structure values...

|-------t3[0]---------| |--------t3[1]--------|
| |-t2[0]-| |-t2[1]-| | | |-t2[0]-| |-t2[1]-| |
 {  0 , 2  ,  4 , 6  } , {  1 , 3  ,  5 , 7  }
    |   |     |   t1[1]     |   |     |   t1[1]
    |   |    t1[0]          |   |    t1[0]
    |  t1[1]                |  t1[1]
   t1[0]                   t1[0]

Edited by scudzilla

0

That's exactly what I needed! Thank you so much.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.