help: unnamed unions within structures 
Author Message
 help: unnamed unions within structures

Hi!  I've recently encountered some code with the following structure
declaration:

typedef struct mystruct         {
        int a;
        union {
                char b[20];
                struct {
                        int c;
                        int d;
                } e;
        };

Quote:
} mystruct;

the above was used in the following manner:
f()
{
        int Off;
.
.
        Off = offsetof(mystruct, e.c);
.
.

Quote:
}

i tried compiling using gcc but it terminates with an error indicating that
e is not a member of mystruct.

has anyone ever successfully used this kind of structure declaration?

barry



Mon, 29 Jan 2001 03:00:00 GMT  
 help: unnamed unions within structures

Quote:
> Hi!  I've recently encountered some code with the following structure
> declaration:

> typedef struct mystruct    {
>    int a;
>    union {
>            char b[20];
>            struct {
>                    int c;
>                    int d;
>            } e;
>    };
> } mystruct;

> the above was used in the following manner:
> f()
> {
>    int Off;
> .
> .
>    Off = offsetof(mystruct, e.c);
> .
> .
> }

> i tried compiling using gcc but it terminates with an error indicating that
> e is not a member of mystruct.

> has anyone ever successfully used this kind of structure declaration?

> barry

<Jack>

That's very good of gcc for a change.  The C standard does not allow anonymous
unions anywhere.  C++ allows them in some circumstances, but I don't know if
this is one of them.  Even in C++ I am not sure how you would access members
of the union, because just like the message says, e is not a member of
mystruct.

e is a member of the union and the union is a member of mystruct.

Was this C++ code to begin with?  Can you just give the union a name?

</Jack>



Mon, 29 Jan 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Union within structures

2. unions within structures

3. unnamed UNIONS

4. unnamed struct/union in C99

5. Help: BC5 anonymous structures, unions

6. structures within structures

7. Q about structures within structures

8. Initialising Structure Within Structure...How?!

9. Help in initializing function pointers within structures

10. help: accessing a pointer to a list within an array of structures

11. structuresand arrays within unions

12. Question regarding unions and structures

 

 
Powered by phpBB® Forum Software