Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #158376

Re: Anonymous unions

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: Anonymous unions
Date 2021-01-15 10:10 -0800
Organization A noiseless patient Spider
Message-ID <86zh1anka3.fsf@linuxsc.com> (permalink)
References <rtrkml$1de$1@gioia.aioe.org>

Show all headers | View raw


Steve Keller <keller.steve@gmx.de> writes:

> I want to define something like the following:
>
>     struct foo {
>         int a;
>     int b;
>     };
>
>     struct bar {
>         ...;
>     union {
>         int        a;
>         struct foo f;
>     };
>     ...;
>     };
>
> Having a struct bar b, is it correct and that b.a and b.f.a is
> always the same object?  I.e. will the following always work?
>
>     struct bar b;
>     struct foo f = { 42, 21 };
>     b.f = f;
>     if (b.a == 42)
>         puts("worked");
>
> I have tried with gcc and it works.  But does standard C guarantee
> this?

The short answer is yes.

A somewhat longer answer would say that anonymous unions have been
supported in standard C only since C11.

Also, technically the objects b.a and b.f.a are distinct objects,
but they are required to occupy the same area of memory, which is
(part of) what I think you're asking.

Furthermore the read access to b.a is well defined, and required
to work as you expect after the store into b.f.  (It is not, as
some may have suggested, implementation-defined behavior.)

Having said that, I might suggest some amount of caution is in
order, because the case is unusual enough so some compilers may
get it wrong.  I know that isn't what you are asking, and it may
turn out not to be a problem at all, but it is something that you
may want to take into consideration.  Disclaimer:  I have no
actual experience with something like this being a problem, and
I would expect any major compiler such as gcc or clang always to
act as you expect.  Despite that, a word to the wise seemed in
order.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Anonymous unions Steve Keller <keller.steve@gmx.de> - 2021-01-15 09:45 +0100
  Re: Anonymous unions "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-01-15 01:41 -0800
    Re: Anonymous unions Sumireko Usami <usasumi3hu@gmail.com> - 2021-01-15 13:00 +0100
      Re: Anonymous unions "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-01-16 14:04 -0800
    Re: Anonymous unions Bart <bc@freeuk.com> - 2021-01-15 12:38 +0000
      Re: Anonymous unions Bonita Montero <Bonita.Montero@gmail.com> - 2021-01-15 14:18 +0100
        Re: Anonymous unions Thiago Adams <thiago.adams@gmail.com> - 2021-01-15 06:37 -0800
          Re: Anonymous unions "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-01-16 14:05 -0800
            Re: Anonymous unions "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-01-16 14:08 -0800
          Re: Anonymous unions Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-01-16 19:04 -0800
            Re: Anonymous unions "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-01-16 19:32 -0800
  Re: Anonymous unions Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-01-15 10:10 -0800
  Re: Anonymous unions Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-01-15 19:48 -0800

csiph-web