472,371 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

union initializers idiosyncrasies


Hello,

I would like to know what the C standards (and in particular the C99
standard) have to say about union initializers with regards to the
following code snippet (which compiles fine under gcc 3.2.2 but
does not produce the expected results, the expected results
being the ones annotated in the comments in the code):

#include <stdlib.h>

union Foo {
struct {
int y1;
union {
int z1;
float z2;
} y2;
} x;
void *a;
};

int main(void) {
union Foo foobar1 = { { 1, { 22 } } };
union Foo foobar2 = { { 2, { 33.1 } } };
/* expecting 1, OK */
printf("foobar1: %d\n", foobar1.x.y1);
/* expecting 22, OK */
printf("foobar1: %d\n", foobar1.x.y2.z1);
/* expecting 2, OK */
printf("foobar2: %d\n", foobar2.x.y1);
/* expecting 33.1, not 0.00 */
printf("foobar2: %f\n", foobar2.x.y2.z2);
}

I would have thought, that seeing that 33.1 is a float and not an
integer, the float would have been properly stored in the union.
What does the standard say about the above code; is any of it
illegal or is any of it subject to implementation dependent
behavior, and if so then why didn't gcc complain?

Thank you for your clarifications,

Neil

Nov 13 '05 #1
6 3216
On Tue, 16 Sep 2003 23:33:50 -0230, Neil Zanella <nz******@cs.mun.ca>
wrote in comp.lang.c:

Hello,

I would like to know what the C standards (and in particular the C99
standard) have to say about union initializers with regards to the
following code snippet (which compiles fine under gcc 3.2.2 but
does not produce the expected results, the expected results
being the ones annotated in the comments in the code):

#include <stdlib.h>

union Foo {
struct {
int y1;
union {
int z1;
float z2;
} y2;
} x;
void *a;
};

int main(void) {
union Foo foobar1 = { { 1, { 22 } } };
union Foo foobar2 = { { 2, { 33.1 } } };
/* expecting 1, OK */
printf("foobar1: %d\n", foobar1.x.y1);
/* expecting 22, OK */
printf("foobar1: %d\n", foobar1.x.y2.z1);
/* expecting 2, OK */
printf("foobar2: %d\n", foobar2.x.y1);
/* expecting 33.1, not 0.00 */
printf("foobar2: %f\n", foobar2.x.y2.z2);
}

I would have thought, that seeing that 33.1 is a float and not an
integer, the float would have been properly stored in the union.
What does the standard say about the above code; is any of it
illegal or is any of it subject to implementation dependent
behavior, and if so then why didn't gcc complain?

Thank you for your clarifications,

Neil


The nesting of unions has nothing at all to do with this, the results
would be the same if y2 were defined as a separate union.

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.

Consider this:

union x
{
int i; long l; float f; double d; long double ld;
};

union x q = { 1 }; /* initialize the int or the long? */
union x r = { 1.0 }; /* float, double, or long double? */

Why should you expect an error for the second initialization? There
is nothing illegal about this:

int i = 3.14159;

....although as a QOI issue you might like to see a warning from the
compiler. If it actually refused to compile it, the compiler would be
broken.

The only time such code is illegal is if the initializer is not
assignment compatible (without a cast) with the type being
initialized:

char *cp = 3.14159;

....requires a diagnostic.

C99 has added designated initializers which allow you to initialize
any specific member of a union, but the syntax is quite different.

--
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
Nov 13 '05 #2
Hello,

Thank you for your detailed reply...

Jack Klein <ja*******@spamcop.net> wrote in message:
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.
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:
C99 has added designated initializers which allow you to initialize
any specific member of a union, but the syntax is quite different.


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 ---

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.

Thanks you for your feedback and contributions,

Neil
Nov 13 '05 #3
Hello,

Here is a very simple way to fix the problem in C99:
#include <stdlib.h>

union Foo {
struct {
int y1;
union {
int z1;
float z2;
} y2;
} x;
void *a;
};

int main(void) {
union Foo foobar1 = { { 1, { 22 } } };
/* union Foo foobar2 = { { 2, { 33.1 } } }; */
union Foo foobar2 = { { 2, { .z2 = 33.1 } } };
/* expecting 1, OK */
printf("foobar1: %d\n", foobar1.x.y1);
/* expecting 22, OK */
printf("foobar1: %d\n", foobar1.x.y2.z1);
/* expecting 2, OK */
printf("foobar2: %d\n", foobar2.x.y1);
/* expecting 33.1, not 0.00 */
printf("foobar2: %f\n", foobar2.x.y2.z2);
/* output: 33.099998: OK (with usual but unrelated roundoff error) */
}


From the C99 standard:

--- begin quote ---
Forward, paragraph 5, page xiii
- relaxed constraints on aggregate and union initializations
--- end quote ---

I guess this is what this remark refers to.

Regards,

Neil
Nov 13 '05 #4
On 17 Sep 2003 18:19:31 -0700, nz******@cs.mun.ca (Neil Zanella) wrote
in comp.lang.c:
Hello,

Thank you for your detailed reply...

Jack Klein <ja*******@spamcop.net> wrote in message:
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.
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:
C99 has added designated initializers which allow you to initialize
any specific member of a union, but the syntax is quite different.


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 ---


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.
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.
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;
Thanks you for your feedback and contributions,

Neil


--
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
Nov 13 '05 #5
Thanks Jack,

Your lucid explanation explains very well the semantic issues I brought
up on this newsgroup pertaining to union initialization and assignment.
Please don't take things personally. Basically I was looking for
something in the standard stating what you wrote here:

--- begin Jack's quote ---
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.
--- end Jack's quote ---

That's what I expected, even though in pre-C99 initializer lists it
is the first element in the union's definition that gets assigned,
which is what made me wonder about other kind of initializations.
Basically what I was trying to get at is that the standard should
perhaps have included something explicit to reflect what you
describe in the above quote, which is what we'd all expect.
C can be quirky, and unless the standard documents it, you
never know whether what you're doing is portable.

Thank you and have a nice day!

Neil

Jack Klein <ja*******@spamcop.net> wrote in message news:<3e********************************@4ax.com>. ..
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.
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.


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;
Thanks you for your feedback and contributions,

Neil

Nov 13 '05 #6
On 17 Sep 2003 18:25:54 -0700, nz******@cs.mun.ca (Neil Zanella)
wrote:
<snip>
union Foo foobar2 = { { 2, { .z2 = 33.1 } } }; <snip> From the C99 standard:

--- begin quote ---
Forward, paragraph 5, page xiii
- relaxed constraints on aggregate and union initializations
--- end quote ---

I guess this is what this remark refers to.

No, that item refers to 6.7.8p3, which in C90 also applied to "an
initializer list for an object that has aggregate or union type" even
if automatic; pre-Standard such automatic variables might not even be
initializable at all -- FAQ 1.31.

The item "— designated initializers" on pxii refers to what you did.
Hint: 6.7.8, which is entirely about "initialization" and
"initializer"s, consistently uses "designation" and "designated" for
this new feature.

- David.Thompson1 at worldnet.att.net
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
73
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
4
by: John Devereux | last post by:
Hi, gcc has started warning about the lack of inner braces in initializers like :- struct io_descriptor { int number; char* description;
4
by: Michael Brennan | last post by:
I have a menu_item structure containing an union. func is used if the menu item should use a callback, and submenu if a popupmen should be shown. struct menu_item { enum { function, popup }...
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
4
by: Theo R. | last post by:
Hi all, I have the following struct defined - #define INTEGER 0 #define STRING 1 typedef struct { char type ; union {
4
by: benn686 | last post by:
I have a structure that contains a union that Id like to initialize at compile time... something like: //global declare and initialize fullStructType var1 = { unionMember.union1.field1...
1
by: r035198x | last post by:
Inspiration Inspired by a post by Jos in the Java forum, I have put together a little article on class initializers. Initializers are indeed underutilized by most Java programmers. Once the Java...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.