Alexander
Tue May 11 23:07:07 CDT 2004
Note also that is you compile case 2 (nested structure with tag, but no
name) with MSC compiler, in C code it will result in a structure body
included to the outer structure layout. In C++ code, of course, it will just
define a structure type inside the outer structure namespace.
This is why such constructs are better avoided in C code.
"Shailesh Humbad" <humbads1@hotmail.com> wrote in message
news:eJk%23G04NEHA.3124@TK2MSFTNGP12.phx.gbl...
> John Carson wrote:
> >
> > MSDN calls this second form an "unnamed anonymous struct". Other
sources,
> > however, call it simply an "anonymous struct" and DON'T use that term
for
> > the first form.
> >
> > The C++ standard does NOT use the Microsoft terminology. It allows for
an
> > "anonymous union", by which it means what MSDN calls an "unnamed
anonymous
> > union".
> >
> >
> At this URL:
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/class_5.asp
>
> It says, "
> A Microsoft C extension allows you to declare a structure variable
> within another structure without giving it a name. These nested
> structures are called anonymous structures. C++ does not allow
> anonymous structures.
> You can access the members of an anonymous structure as if they were
> members in the containing structure.
> "
>
> The (condensed) example is:
> ----------------------
> struct phone
> {
> long number;
> };
>
> struct person
> {
> struct phone; // Anonymous structure; no name needed
> } Jim;
> ----------------------
> The "phone" declaration can be moved into the "person" declaration.
> ----------------------
> struct person
> {
> struct phone
> {
> long number;
> };
> } Jim;
> ---------------------
> But now that it's enclosed, there is probably no need to name it:
> ---------------------
> struct person
> {
> struct
> {
> long number;
> };
> } Jim;
> ----------------------
>
> Correct me if I'm wrong, but with structs, there are four combinations:
>
> (1) type name excluded, identifier excluded - MSFT C Extension
> (2) type name included, identifier excluded - MSFT C Extension
> (3) type name excluded, identifier included - C/C++ Standard
> (4) type name included, identifier included - C/C++ Standard
>
> With unions, all four combinations are allowed in the C/C++ standard.
> It would be nice to have some standardized terminology to describe
> each of these constructs. From what has been stated so far, my best
> guess is:
>
> (1) MSFT unnamed anonymous struct; C/C++ undefined
> (2) MSFT and C/C++ - struct definition only
> (3) MSFT unnamed struct; C/C++ struct
> (4) MSFT and C/C++ - struct definition with declaration
>
>