2

This is bizarre. I haven't read anywhere that defining a structure inside another structure definition allowed in C. But this link says its allowed.

http://www.c4learn.com/structure-within-structure-nested.html

Is this true?

mlemboy
  • 387
  • 2
  • 3
  • 15

3 Answers3

2

Yes, you can declare nested structure. Here is the syntax:

C11 (n1570), § 6.7.2.1 Structure and union specifiers

struct-or-union-specifier:
    struct-or-union identifier (opt) { struct-declaration-list }
    struct-or-union identifier

struct-declaration-list:
    struct-declaration
    struct-declaration-list struct-declaration
Community
  • 1
  • 1
md5
  • 23,373
  • 3
  • 44
  • 93
  • 2
    Those two definitions don't really answer the question without explaining what all of the other grammar elements are. Furthermore, the dry grammar of the language spec doesn't help a new programmer learn what's going on, and C11 isn't very widespread yet, so an answer for C89 or C99 would be much more helpful. – Adam Rosenfield Jul 18 '13 at 15:49
2

Inception! We must go deeper!!

struct EvenDeeper {
    int a, b, c;
    struct {
        char a;
    } u;
};

struct Inner {
    struct EvenDeeper e;
};

struct Outer {
    struct Inner i;
};

int main(int argc, char *argv[]) {
    struct Outer o;
    o.i.e.a = 5;
    o.i.e.b = 4;
    o.i.e.c = 3;
    o.i.e.u.a = '?';

    printf("%d:%d:%d:%c", o.i.e.a, o.i.e.b, o.i.e.c, o.i.e.u.a);

    return 0;
}

Compiles properly with "gcc -std=c89" with output:

5:4:3:?

Struct structure defined in §6.7.2.1

George Mitchell
  • 1,158
  • 2
  • 11
  • 24
1

Yes, you can declare a structure within another structure.
The two downsides I see are:
1. Readability: it might make your code hard for others to read, especially if it's in a team project.
2. The inner structure's will be scoped only to the outer structure, not to mention that(depending on how it's defined), it can only be used once and reusing the same structure will require re-defining it again.

Hope this helps.

Kneel-Before-ZOD
  • 4,141
  • 1
  • 24
  • 26
  • what do u mean by saying that it can only be used once? And re-defining the same struct is an error, right, same as redefining any identifier? – Rich Mar 12 '14 at 19:15
  • @Rich what I meant was that it's similar to using an anonymous class/function; you cannot call the function independently. So, if you have a struct A which contains structs B and C, you cannot use the functionality of struct C without redefining it or calling struct A. – Kneel-Before-ZOD Mar 13 '14 at 13:59
  • In C language, types are global. You can refer to the inner struct outside no problem. In C++, you just use the scope resolution to access the inner struct(class). I don't understand ur reason here. – Rich Mar 13 '14 at 16:04
  • According to the example made above, struct C is localized to struct A, meaning that struct C cannot be independently accessed without struct A. If that is wrong, please, provide me something that proves it wrong. Also, types are global **if/when** defined outside any function. – Kneel-Before-ZOD Mar 13 '14 at 16:48
  • Yeah, I mean the inner struct will have the same scope as the containing scope in C. I misunderstood your post. It made me think that outside the containing structure, you can't even use it. You can only use it with struct A always tagged along. – Rich Mar 13 '14 at 16:51
  • Yeah....that was what I meant. If you have a better way of rephrasing the sentence, please, edit it. I don't want others confused about the intended meaning. – Kneel-Before-ZOD Mar 13 '14 at 17:08