On 17 Sep 2003 18:19:31 -0700,
nzanella@cs.mun.ca (Neil Zanella) wrote
in comp.lang.c:
[color=blue]
> Hello,
>
> Thank you for your detailed reply...
>
> Jack Klein <jackklein@spamcop.net> wrote in message:
>[color=green]
> > You thought incorrectly. Prior to C99, and even under C99 using the
> > syntax you are using, it is only possible to initialize the first
> > member of a union, in definition order. The type of the initializer
> > is irrelevant as long as it is assignment compatible with the
> > destination type.[/color]
>
> The C99 standard discusses the above matter in Section 6.7.8.
> Furthermore,
> page 347 of K.N.King's C book which covers C95 states the following
> about
> unions:
>
> --- begin quote ---
> Like structures, unions can be copied using the = operator, passed to
> functions, and returned to functions.
> --- end quote ---
>
> This covers the legality of using assignment to copy unions. Then it
> states:
>
> --- begin quote ---
> Unions can even be initialized in a manner similar to structures.
> However,
> only the first member of a union can be given an initial value.
> --- end quote ---
>
> This is not true in C99 (see below). Even though only one member of a
> union
> can be initialized in C99 (which makes sense since all union members
> share
> the same storage space), we can now initialize any member of the
> union, not
> only the one that appears first in the union declaration. We do this
> by
> specifying the member name explicitly inside the union initializer
> list.
> From the C99 standard:
>[color=green]
> > C99 has added designated initializers which allow you to initialize
> > any specific member of a union, but the syntax is quite different.[/color]
>
> Yes, from the C99 standard we can see the following example which
> shows
> us how this can be achieved:
>
> Section 6.7.8, paragraph 130, EXAMPLE 13
>
> --- begin quote ---
> union { /* ... */ } u = { .any_member = 42 };
> --- end quote ---
>
> This C99 idiom lends itself to good programming style. As long as we
> use
> member names in union initializer lists we can reorder the members of
> unions in the union definition.
>
> -------------------------------------------------------------------------
>
> What the book does not cover is the following:
>
> suppose I have something like:
>
>
>
> int main(void) {
> union foo { int c; double d; } x;
> /* ... */
> x.d = 3.3;
> /* ... */
> union foo y = x;
> /* let's check that it worked */
> printf("%g\n", x.d);
> /* it works with my compiler, outputs 3.3 with gcc 3.2.2 */
> return 0;
> }
> Here I am initializing union y with an expression, x, which is a
> union.
> Here it is true that d is not the first member of x. Nevertheless, is
> it
> true in this case that y.d == 3.3 after y gets declared as above? The
> C99 standard states the following:
>
> Section 6.7.8, paragraph 13, page 126
>
> --- begin quote ---
> The initializer for a structure or union object that has automatic
> storage duration shall be either an initializer list or a single
> expression that has compatible structure or union type. In the
> latter case, the initial value of the object, including unnamed
> members, is that of the expression.
> --- end quote ---[/color]
You are still way over-thinking this, for some reason. You can
initialize any modifiable lvalue object in C with the value of another
object of compatible type. This has been true since the 1989 ANSI
standard. That is so basic that it is pretty much taken for granted.
When you write:
union some_type foo = { /* some valid initializer */ };
union some_type bar = foo;
....bar winds up being an exact member-wise copy of foo. In a
structure that would mean that each member in the new structure would
have the same value as the corresponding members of the initiating
structure. For a union it means that the new union will be in the
same "state" (my term, not the standard's) as the original one, namely
the one valid member in the union will be the last stored member in
the old union, and it will contain the same value.
[color=blue]
> So what is the value of the expression x in the above example? I guess
> the initial value of the object, that of the expression, is the union
> itself in the above code, and that the above code is legal in C99.[/color]
The above code was legal in C89, and was a common extension at the
time of publication of the first edition of K&R, in 1978. There is
nothing new to C99 about it.
There are differences between initialization and assignment, but they
are not important here. The two lines I wrote above are not the same
as the following three lines, but the result is the same:
union some_type foo = { /* some valid initializer */ };
union some_type bar;
bar = foo;
[color=blue]
> Thanks you for your feedback and contributions,
>
> Neil[/color]
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
ftp://snurse-l.org/pub/acllc-c++/faq