Home > Backend Development > C++ > Are Anonymous Structs Standard in C ?

Are Anonymous Structs Standard in C ?

Susan Sarandon
Release: 2024-12-03 08:02:10
Original
124 people have browsed it

Are Anonymous Structs Standard in C  ?

Are "Anonymous Structs" Standard in C ?

MSDN claims that anonymous structs are non-standard in C , but a closer examination reveals a different scenario. While the C standard defines "unnamed structs," it does not specifically address the concept of "anonymous structs."

Defining Unnamed Structs

The C standard allows for the declaration of unnamed structs, also known as anonymous structs. These structs do not have a name and are typically used as nested types within other structures. The following code snippet demonstrates an unnamed struct:

struct Foo {
    struct {
        int hi;
        int bye;
    };
};
Copy after login

This code defines an unnamed struct within the Foo structure. The unnamed struct consists of two members: hi and bye.

Accessing Members of Anonymous Structs

Standard C allows for the access of members of unnamed structs using the dot operator. For instance, in the previous example, the hi member can be accessed as follows:

Foo f;
f.hi = 3;
Copy after login

Anonymous Structs versus "Unnamed Structs as Members"

While C 03 and C 11 do not explicitly mention "anonymous structs," the phenomenon of accessing members of unnamed structs as if they were members of the parent structure is not specifically addressed. This behavior, known as "anonymous structs as members," is not covered by the standard for unnamed structs.

MSDN's Confusion

MSDN appears to mistakenly conflate unnamed structs with anonymous structs as members. While unnamed structs are standard, anonymous structs as members are a non-standard feature supported by certain compilers such as GCC and Visual C .

Conclusion

In summary, while anonymous structs as members are not standard in C , unnamed structs are fully supported by the standard. The terms "unnamed struct" and "anonymous struct" refer to different concepts, with the latter referring to the non-standard behavior of accessing members of an unnamed struct as if they were members of the parent structure.

The above is the detailed content of Are Anonymous Structs Standard in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template