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!

Recommended Answers

All 2 Replies

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]

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.