Struct question

why is s.c.a outputting the number 3?
why is s.c.b outputting the number 4?

can someone explain it to me?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   #include <iostream>
    using namespace std;
    struct S {
      int  a;
      char b;
      struct {
              float a;
              int   b;
      } c;
    };
    int main (int argc, const char *argv[])
    {
      S s = { 1, 2, 3, 4 };
      cout << s.c.a << s.c.b;  
    }
I am so desperate to understand this thing...
after the line 13, the 4 numbers lists, for some reason, 1 is for int a,
2 is for char b, 3 is for float a, and 4 is for int b? argh
1 is for int a, 2 is for char b, 3 is for float c.a, and 4 is for int c.b. So s.a is 1, s.b=2, s.c={3,4}, s.c.a=3, s.c.b=4. It is a little unfortunate that you have this naming scheme, but what it emphasize is that you can have the same name in different parts of the structures. Think of it by analogy to folders on your computer. You can have c:\pictures, c:\username\pictures. Both folders have the same name, but they point to different paths
Topic archived. No new replies allowed.