Hello.,
I'm reading about anonymus structs and have problems understanding what is the privilege to have one. could anyone demonstrate an example of both anonymus and regular struct. I see that anonymus struct has a name after the the } and before semicolon. Any help would be greatly appreciated.

Thank you

Dani AI

Generated

Anonymous structs/unions let an inner aggregate’s members be injected into the containing aggregate so those inner members can be accessed without a qualifier — essentially a “flattening” convenience useful for variant records or compact layouts. In modern C this is standardized (C11 and later define unnamed/anonymous struct and union members and how their members are treated), but the portability story differs for C++ where only anonymous unions are standard; anonymous struct members remain either non‑standard or compiler extensions in many C++ toolchains. (C struct rules).

A short C11 example (not shown earlier in the thread) that illustrates flattening inside a tagged record:

#include <stdint.h>

struct Message {
    uint8_t type;
    union {
        struct { uint32_t id; uint64_t ts; };   /* members are accessed as msg.id / msg.ts */
        struct { uint8_t flags; uint8_t code; };/* members are accessed as msg.flags / msg.code */
    }; 
};

Access is direct (e.g. msg.id) when the anonymous variant is active. If a member type needs to be referred to elsewhere (self‑references, pointers, forward declarations) give the inner struct a tag — anonymous members cannot be named/forwarded.

Portability and pitfalls: compilers (GCC/Clang/MSVC) commonly provide nameless-field extensions and GCC adopted the C11 behavior; still these constructs can trigger warnings (MSVC C4201) or require GNU/MS extensions to accept nonstandard forms. Avoid ambiguous names (two injected members with the same identifier) and be cautious with flexible arrays/fine layout rules — behavior and diagnostics differ across standards and compiler modes. See GCC’s “Unnamed Fields” notes and the MSVC C4201 warning for details. (GCC docs: https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html) (MSVC C4201).

Clarifying the thread: ’s comment reflected pre‑C11 reality — anonymous structs were not in older C, but C11 added them; was correct that C has no reinterpret_cast (that’s a C++ operator). ’s suggestion to use typedefs/named types is the pragmatic portable alternative when a reusable or self‑referencing type is needed. For cross‑platform code prefer the named‑member form or guard nameless fields behind compile‑time checks for compiler support. (C/C language notes: https://en.cppreference.com/w/c/language/struct; C11/WG14 discussion: ).

Recommended Answers

All 6 Replies

take a look at this

Anonymous structures are not standard C therefore they are implemented as extensions. Anonymous structures are only useful as pointer targets, i.e.
"struct A *foo". You can pass around pointers to structures without
knowing the size of the structure, although you can't dereference them.


Anonymous unions are standard C and they are can be found inside an object or inside a namespace.

1. When an anonymous union is declared inside of an object the role is to have all data members of that class or struct to share the same memory space and they are accessed as any other datamembers. This is not considered a healthy practice and it can be used only with the system that do not have a good support for typecasting. reinterpret_cast is the recommended way over anonymous unions.

2. An anonymous union can be declared inside a namespace or at the global scope but because the unions members require internal linkage the union must be declared static.

commented: C != C++, you should know that by now... -4

An anonymous struct can exist, but since you can't refer to it by a name, you need to instantiate it right away (unlike an anonymous union).

>>reinterpret_cast is the recommended way over anonymous unions.


reinterpret_cast is not allowed in C. So I don't know where you got your information but its wrong.


The only place I've used anonymous structs is inside a union

union something
{
    struct
    {
          int x;
          int y;
    };
    unsigned char buf[2 * sizeof(int)];
}

I think you are right.

The most natural place for structures without tags is typedef declaration (it's the only place I've used anonymous structs ;)):

typedef struct 
{
  ...
} Record;
...
Record  rec;
Record* prec = &rec;
...

Of course, it's impossible to define self-referential structures in a such manner.

Yet another program case (less interesting):

struct { int x, y; } s;
struct { int x, y; } t;

Types of s and t are different. It's impossible to declare other variables of these types or to pass s and t as parameters.

>>The most natural place for structures without tags is typedef declaration

I forgot about those :)

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.