3

I want to know if it is possible to declare anonymous structs in ANSI C. The code I have is:

struct A
{
    int x;
};

struct B
{
    struct A;
    int y;
};

When I compile it I get: warning: declaration does not declare anything

I have read that the flag -fms-extensions does the trick, it however only works on windows systems as it produces: warning: anonymous structs are a Microsoft extension [-Wmicrosoft]

Is there any ANSI equivalent extension that I can use?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Klas. S
  • 650
  • 9
  • 21
  • A `struct` without an identifier? No, I do not think so. You could declare a `struct` without a type name but with an identifier with `struct {} identifier;`. `struct`s' members have a distinct scope, so how would the members be addressed without an identifier? – cadaniluk Dec 24 '15 at 11:29
  • 2
    ANSI C does not have this feature. Are you restricted to ANSI C or could you also use C99 or C11? – fuz Dec 24 '15 at 11:33
  • I can use C99 and C11, is there any solution for those? – Klas. S Dec 24 '15 at 11:45
  • 2
    @KlasSegeljakt Yes. C11 provides anonymous structures. Let me amend my answer. – fuz Dec 24 '15 at 11:46

3 Answers3

3

A trick to get almost this feature in ANSI C is to use an appropriate macro:

struct A {
    int x;
};

struct B {
    struct A A_;
    int y;
};

#define bx A_.x

Then you can simply do

struct B foo, *bar;

foo.bx;
bar->bx;

In C11 though, anonymous structures are supported and you can simply do

struct B {
    struct {
        int x;
    };

    int y;
}

but sadly not

struct A {
    int x;
};

struct B
{
    struct A;
    int y;
};

As the anonymous structure has to be declared inside the structure it is anonymous to.

See this answer for more details on anonymous members in C11.

Community
  • 1
  • 1
fuz
  • 88,405
  • 25
  • 200
  • 352
3

Its possible to declare anonymous struct and union. The ISO C11 added this feature and GCC allows it as an extension.

C11 section §6.7.2.1 para 13:

An unnamed member whose type specifier is a structure specifier with no tag is called an anonymous structure; an unnamed member whose type specifier is a union specifier with no tag is called an anonymous union. The members of an anonymous structure or union are considered to be members of the containing structure or union. This applies recursively if the containing structure or union is also anonymous.

19 The following illustrates anonymous structures and unions:

struct v {
    union { // anonymous union
          struct { int i, j; }; // anonymous structure
          struct { long k, l; } w;
    };
    int m; 
} v1; 

v1.i = 2;   // valid
v1.k = 3;   // invalid: inner structure is not anonymous
v1.w.k = 5; // valid 

Now b can be accessed by just using foo.b.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 4
    You should clarify that this is new in C11 as OP is talking about ANSI C. And also, thanks for replicating the content of my answer in a condensed form. – fuz Dec 24 '15 at 11:52
  • @FUZxxl; It may seem to you that the content was taken from your answer but it is not. I took that example from [here](https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html) and changed that slightly. BTW, changed the whole content now. – haccks Dec 24 '15 at 12:05
  • 1
    I don't say that you took my answer. I say that your answer is redundant as it says the same things (somewhat condensed) I say in my answer. But whatever, I shouldn't care. – fuz Dec 24 '15 at 12:07
-1

You want something like this i suppose:

struct B {
    struct {
        int x;
    } A;
    int y;
};

And you could do:

struct B b;
b.A.x = 5;
printf( "%d\n", b.A.x );
Popiel
  • 183
  • 3
  • 13
  • 3
    No, that's not anonymous: You still have to do `foo.A.x` instead of the wanted `foo.x`. – fuz Dec 24 '15 at 11:45