Anonymous sub-structures and GCC 
Author Message
 Anonymous sub-structures and GCC

Hello,
        apologies if this is in the wrong newsgroup, or in the FAQ (I did have
a quick look)

consider the declaration

typedef struct
{
        int     a;
        union
        {
                int x;
                long y;
        };
        int b;

Quote:
} my_struct;

void my_func(void)
{
        my_struct str1;

        str1.x = 0;

Quote:
}

Now for my questions:

Is this legal? (Micro$oft manages to compile it, as do a few others I have tried)
        Its obviously not very portable.

How can I make GCC compile it?
        I get a warning in the declaration (anonymous struct/union declares no instances)
        I get an error when I try to use it (my_struct doesnt contain an element 'x')

thank you for listening, any help appreciated.

Majik.

--
--
-- Majik - Owner and Curator of the Sinclair Microcomputer Museum
--                      ' Spod in a suitcase. Have compiler, will travel '
--       http://www.*-*-*.com/ ~majik/sinclair/
--



Tue, 03 Jul 2001 03:00:00 GMT  
 Anonymous sub-structures and GCC
[example trimmed for simplicity -- rgs]

Quote:

>typedef struct
>{
> union
> {
>  int x;
>  long y;
> };
>} my_struct;

>void my_func(void)
>{
> my_struct str1;
> str1.x = 0;
>}

>Now for my questions:

>Is this legal?

No.  The declaration of the union inside the structure is legal, but
useless -- you don't create anything of that union type and you don't give
the union type a tag, so you can't refer to it later.

However, the reference to str1.x is not legal simply because x is not a
member of str1.  It's a member of the union type declared inside my_struct,
but that's a quite different thing.  This is analogous to doing

  void bar(void)
  {
    union {
      int foo;
    };

    foo = 42;
  }

which is obviously not legal.

Quote:
>(Micro$oft manages to compile it, as do a few others I have tried)

Yes, there is a Microsoft-specific extension which allows this.  It just
promotes the members of the enclosed struct or union to be members of the
enclosing one.  This is duly flagged as an MS extension in the online help.

Quote:
>How can I make GCC compile it?

Fix the broken code!  :-)

Quote:
> I get a warning in the declaration (anonymous struct/union declares no

instances)

Right, this is warning you that the enclosed union type declaration is
useless: it has no tag (it's anonymous) and you don't declare anything with
that type.

Quote:
> I get an error when I try to use it (my_struct doesnt contain an element

'x')

Right, it doesn't contain an element 'x' as explained above.

Maybe you wanted to do this:

Quote:
>typedef struct
>{
> union
> {
>  int x;
>  long y;
> } z;
>} my_struct;

>void my_func(void)
>{
> my_struct str1;
> str1.z.x = 0;
>}

Now you've created something, 'z', of this union type and embedded it as a
member of the struct.  str1 therefore has a member z, which is of union type
and in turn has a member x -- so the whole thing can be referred to as
str1.z.x.  Of course, this works for str1.z.y as well.  This may give you
the effect you wanted (depending on exactly what you were trying to do).

Cheers,
Richard



Tue, 03 Jul 2001 03:00:00 GMT  
 Anonymous sub-structures and GCC
Aren't anonymous unions C++ only?
If this is a crass statement forgive me I definitely
wouldn't call myself an expert!

Pete Gates



Tue, 03 Jul 2001 03:00:00 GMT  
 Anonymous sub-structures and GCC


Quote:
>Hello,
>        apologies if this is in the wrong newsgroup, or in the FAQ (I did have
>a quick look)

>consider the declaration

>typedef struct
>{
>        int     a;
>        union
>        {
>                int x;
>                long y;
>        };
>        int b;
>} my_struct;

>void my_func(void)
>{
>        my_struct str1;

>        str1.x = 0;
>}

>Now for my questions:

>Is this legal?

No, this is a syntax error in C. A member declaration must contain a member
name.

Quote:
>(Micro$oft manages to compile it, as do a few others I have
> tried)

Try using the -Za option to disable extensions.

Quote:
>        Its obviously not very portable.

>How can I make GCC compile it?

Specify a name for the union member and update the code that uses it
accordingly.

--
-----------------------------------------


-----------------------------------------



Tue, 03 Jul 2001 03:00:00 GMT  
 Anonymous sub-structures and GCC

Quote:

>No, this is a syntax error in C. A member declaration must contain a
>member name.

You're right.  I was in error (in a separate reply).

Cheers,
Richard



Fri, 06 Jul 2001 03:00:00 GMT  
 Anonymous sub-structures and GCC
On Fri, 15 Jan 99 18:46:20 GMT, Lawrence Kirby

Quote:

>No, this is a syntax error in C. A member declaration must contain a member
>name.

Nit: Except if the member is a bitfield.

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>



Fri, 06 Jul 2001 03:00:00 GMT  
 Anonymous sub-structures and GCC


Quote:
>On Fri, 15 Jan 99 18:46:20 GMT, Lawrence Kirby

>>No, this is a syntax error in C. A member declaration must contain a member
>>name.

>Nit: Except if the member is a bitfield.

Very true.

--
-----------------------------------------


-----------------------------------------



Fri, 06 Jul 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Erroneous structures (was anonymous structures)

2. Anonymous unions in C with gcc?

3. Anonymous unions in gcc

4. Help: BC5 anonymous structures, unions

5. accessing sub structure elements through a function

6. Sub Sub Projects in VC4.2

7. C declaration (structure alignment tricks) fails under VC++ but work under gcc

8. Structure Alignment in GCC

9. structure alignment and GCC

10. "tree" data structure of GCC

11. GCC structure alignment query

12. gcc: how to not align structures...

 

 
Powered by phpBB® Forum Software