Re: [patch] ptype: show members of an unnamed struct inside an union

Daniel Jacobowitz
2007-08-29T16:30:37Z

On Tue, Aug 28, 2007 at 09:36:33PM -0700, Joel Brobecker wrote:
> > > Does this case actually make any sense? I don't think you can access
> > > this union in your C program either, or can you?
> >
> > GCC supports anonymous unions. There's a test for it in the GDB
> > testsuite, too.
>
> But how can you access this union if the field doesn't have a name?
> More precisely, using the example that Carlos sent:
>
> struct my_page {
> union {
> struct { int mapping; };
> };
> };
>
> How do you access the union?

In C, it would be "my_page_var.mapping". The union is transparent.
I think GDB has some failures for anon-union.exp with recent GCC
related to this...

More interesting example. First you have:

struct s {
int x;
int y;
};

Later you switch to:

struct s {
int x;
union {
int y;
void *z;
};
};

The anonymous union lets s.y keep working.

--
Daniel Jacobowitz
CodeSourcery