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


Groups > comp.lang.c > #10202

Order of UNION members (was Re: UNION global variabl initialize)

Date 2011-09-13 16:29 -0400
From Kenneth Brody <kenbrody@spamcop.net>
Newsgroups comp.lang.c
Subject Order of UNION members (was Re: UNION global variabl initialize)
References <8808246d-5605-4a5d-b8a2-ae7267769aed@glegroupsg2000goo.googlegroups.com> <chine.bleu-E5236F.22342012092011@news.eternal-september.org>
Message-ID <1omdnQdZDazPIPLTnZ2dnUVZ_rGdnZ2d@bestweb.net> (permalink)

Show all headers | View raw


On 9/13/2011 1:34 AM, China Blue Corn Chips wrote:
> In article
> <8808246d-5605-4a5d-b8a2-ae7267769aed@glegroupsg2000goo.googlegroups.com>,
>   "Lung.S.Wu"<lung.s.wu@gmail.com>  wrote:
>
>>  From ANSI C definition, section 6.7.8, with UNION, it says that only the
>> first named element will be initialized if it is not initialized explicitly.
>> That means, such the code
[...]
>> Only the member u.a is initialized as 0, And others are undefined.
>
> If you want to depend on an initialisation, do so explicitly,
>      UU u = {.c = 0}
[...]

Question:

If one defines two different unions, consisting of the same members, but in 
a different order, are they guaranteed to be equivalent?

union foo {
     int i;
     float f;
     char *pt;
     };

union bar {
     char *pt;
     int i;
     float f;
     };

Given 6.7.2.1p14, I would think so:

> A pointer to a union object, suitably converted, points to each of its
> members (or if a member is a bitfield, then to the unit in which it
> resides), and vice versa.

Of course, doing so, just to make the "correct" member be initialized to 
zero, would be "unusual" to say the least.

-- 
Kenneth Brody

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


Thread

UNION global variabl initialize "Lung.S.Wu" <lung.s.wu@gmail.com> - 2011-09-12 09:38 -0700
  Re: UNION global variabl initialize jt@toerring.de (Jens Thoms Toerring) - 2011-09-12 17:08 +0000
  Re: UNION global variabl initialize Kenneth Brody <kenbrody@spamcop.net> - 2011-09-12 15:02 -0400
    Re: UNION global variabl initialize "Lung.S.Wu" <lung.s.wu@gmail.com> - 2011-09-12 22:10 -0700
      Re: UNION global variabl initialize Ike Naar <ike@iceland.freeshell.org> - 2011-09-13 07:10 +0000
        Re: UNION global variabl initialize jt@toerring.de (Jens Thoms Toerring) - 2011-09-13 11:42 +0000
          Re: UNION global variabl initialize Robert Wessel <robertwessel2@yahoo.com> - 2011-09-13 13:34 -0500
  Re: UNION global variabl initialize China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-09-12 22:34 -0700
    Re: UNION global variabl initialize "Lung.S.Wu" <lung.s.wu@gmail.com> - 2011-09-12 23:07 -0700
      Re: UNION global variabl initialize China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-09-12 23:35 -0700
      Re: UNION global variabl initialize James Kuyper <jameskuyper@verizon.net> - 2011-09-13 08:09 -0400
    Order of UNION members (was Re: UNION global variabl initialize) Kenneth Brody <kenbrody@spamcop.net> - 2011-09-13 16:29 -0400
      Re: Order of UNION members (was Re: UNION global variabl initialize) James Kuyper <jameskuyper@verizon.net> - 2011-09-13 17:45 -0400
        Re: Order of UNION members (was Re: UNION global variabl initialize) "Lung.S.Wu" <lung.s.wu@gmail.com> - 2011-09-13 19:32 -0700
          Re: Order of UNION members (was Re: UNION global variabl initialize) James Kuyper <jameskuyper@verizon.net> - 2011-09-14 06:59 -0400
            Re: Order of UNION members (was Re: UNION global variabl initialize) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-14 15:08 +0100
        Re: Order of UNION members (was Re: UNION global variabl initialize) Kenneth Brody <kenbrody@spamcop.net> - 2011-09-14 12:50 -0400
          Re: Order of UNION members (was Re: UNION global variabl initialize) James Kuyper <jameskuyper@verizon.net> - 2011-09-14 13:21 -0400

csiph-web