anonymous structs and unions : legal? 
Author Message
 anonymous structs and unions : legal?

I took a quick glance through Harbison & Steele and Schildt's (old) Standard
and couldn't find an answer one way or the other.  Is support of unnamed
stucts and unions a non-standard compiler extension?


Mon, 11 Aug 2003 21:19:35 GMT  
 anonymous structs and unions : legal?

Quote:

> I took a quick glance through Harbison & Steele and Schildt's (old) Standard
> and couldn't find an answer one way or the other.  Is support of unnamed
> stucts and unions a non-standard compiler extension?

yes, unless you're talking about

typedef struct {
    int i;

Quote:
} Foo;

I'm not sure if that counts as anonymous. This is what I consider an anonymous
struct:

typedef struct lock
{
    int locked;

Quote:
} Lock;

typdef struct data
{
    int i;
    char buf[BUFSIZ];
    Lock;    /* anonymous */

Quote:
} Data;

For an interesting example of anonymous structs in C, see
http://plan9.bell-labs.com/sys/doc/comp.html under the heading "Extensions" and
http://plan9.bell-labs.com/sys/doc/compiler.html, section 3.3.

        david

--
fortran was the language of choice
for the same reason that three-legged races are popular.
        -- Ken Thompson, "Reflections on Trusting Trust"



Mon, 11 Aug 2003 21:12:12 GMT  
 anonymous structs and unions : legal?

Quote:


>> I took a quick glance through Harbison & Steele and Schildt's (old) Standard
>> and couldn't find an answer one way or the other.  Is support of unnamed
>> stucts and unions a non-standard compiler extension?

>yes, unless you're talking about

>typedef struct {
>    int i;
>} Foo;

What about:

struct {
    int a, b;

Quote:
} c;

union {
    long d;
    double e;

Quote:
} f;

Aren't they unnamed (the structs / unions - that is - not the objects) ?

Stefan



Tue, 12 Aug 2003 02:02:21 GMT  
 anonymous structs and unions : legal?

Quote:

>I took a quick glance through Harbison & Steele and Schildt's (old)
Standard
>and couldn't find an answer one way or the other.  Is support of unnamed
>stucts and unions a non-standard compiler extension?

Yes.

--
Peter Pichler (Increment my "From:" address if your email bounces.)



Mon, 11 Aug 2003 23:01:27 GMT  
 anonymous structs and unions : legal?


Quote:
> For an interesting example of anonymous structs in C, see
> http://plan9.bell-labs.com/sys/doc/comp.html under the heading
"Extensions" and
> http://plan9.bell-labs.com/sys/doc/compiler.html, section 3.3.

Thanks. that was the usage I had in mind, where you might say,

struct {
    int some_flag;
   char* some_string;
    union {
        struct {
            float foo;
            float bar;
        }
        struct {
            int blivit;
            int limpit;
         };
    };

Quote:
} trashy_example;

I've occasionally run into this, and have to go in and assign names to keep
gcc happy. I'd always assumed it was an extension. In a way, it is a nice
feature if you really don't have
a meaningful name for the internals.


Tue, 12 Aug 2003 10:24:11 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Anonymous union in VC++ 7

2. about "anonymous union" problem

3. Anonymous unions

4. Anonymous unions in C with gcc?

5. Help: BC5 anonymous structures, unions

6. Anonymous unions in gcc

7. SizeOf Misreporting Struct w/ Bitfield, Union, and Nested Struct

8. anonymous struct

9. tricky casting question with anonymous structs

10. Initialize array from members of a struct: Legal ?

11. Q. on legal uses of struct return

12. struct/union/pointer query (Why isn't this legal?)

 

 
Powered by phpBB® Forum Software