unions: initialization and assignment

N

Neil Zanella

Hello,

I would like to know whether the following C fragment is legal in
standard C and behaves as intended under conforming implementations...

union foo {
char c;
double d;
};

int main(void) {
union foo x;
x.d = 3.3;
printf("%g\n", x.d);
union foo y = x;
printf("%g\n", y.d);
union foo z;
z = y;
printf("%g\n", z.d);
}

The above syntactic sugar could save a few characters of code if legal
since union members would not have to be explicitly mentioned in the code.

Output (gcc 3.2.2):

3.3
3.3
3.3

Thanks!

Neil
 
J

Jack Klein

Hello,

I would like to know whether the following C fragment is legal in
standard C and behaves as intended under conforming implementations...

union foo {
char c;
double d;
};

int main(void) {
union foo x;
x.d = 3.3;
printf("%g\n", x.d);
union foo y = x;
printf("%g\n", y.d);
union foo z;
z = y;
printf("%g\n", z.d);
}

The above syntactic sugar could save a few characters of code if legal
since union members would not have to be explicitly mentioned in the code.

Output (gcc 3.2.2):

3.3
3.3
3.3

Thanks!

Neil

What exactly are you trying to get at with all of these questions
about unions?

You can assign one union to another in C, this has been part of the
language since the original 1989 ANSI standard.

A union can contain only one member at a time, namely the one that was
last assigned to. If you assign that union to another union, that
same member is still valid in the new union and has the same value.

What is there that you think is so unusual about unions that you would
not expect that code?

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

Pieter Droogendijk

Hello,

I would like to know whether the following C fragment is legal in
standard C and behaves as intended under conforming implementations...

union foo {
char c;
double d;
};

int main(void) {
union foo x;
x.d = 3.3;
printf("%g\n", x.d);
union foo y = x;
#1 whoops, declaration among expressions, not allowed in C89
printf("%g\n", y.d);
union foo z;
#2 whoops, declaration among expressions, not allowed in C89
z = y;
printf("%g\n", z.d);
}
The above syntactic sugar could save a few characters of code if legal
since union members would not have to be explicitly mentioned in the code.

If you mean structure/union assignments, yes, they are valid in C89 and up.

From the C89 rationale:

3.3.2.3 Structure and union members

Since the language now permits structure parameters, structure assignment and
functions returning structures, the concept of a structure expression is now
part of the C language. A structure value can be produced by an assignment, by
a function call, by a comma operator expression or by a conditional operator
expression:

s1 = (s2 = s3)
sf(x)
(x, s1)
x ? s1 : s2
 
N

Neil Zanella

Pieter Droogendijk said:
If you mean structure/union assignments, yes, they are valid in C89 and up.

From the C89 rationale:

3.3.2.3 Structure and union members

Since the language now permits structure parameters, structure assignment and
functions returning structures, the concept of a structure expression is now
part of the C language. A structure value can be produced by an assignment,
by a function call, by a comma operator expression or by a conditional
operator expression:

s1 = (s2 = s3)
sf(x)
(x, s1)
x ? s1 : s2

But the above quote from 3.3.2.3 from the C89 rationale does not state whether
the same applies to unions (besides structures). Does it?

Furthermore, it does not mention the ins and outs of initializing a union at
declaration time rather than through an assignment, either via another union
variable or via an initializer list. Is this at all legal in C89? How about
in C99?

Jack Klein said:
You can assign one union to another in C, this has been part of the
language since the original 1989 ANSI standard.

You claim this is true, but the above quote of the C89 rationale by Pieter
only talks about structures, not unions, so I am not entirely convinced.
And what if the union contains other structures and unions. Does assignment
of compatible unions still work in such cases, according to the C standard?
A union can contain only one member at a time, namely the one that was
last assigned to.

Any book that mentions unions states the above: that's what a union is.
If you assign that union to another union, that same member is still
valid in the new union and has the same value.

This is what I was hoping for, but does the standard even mention this?
What is there that you think is so unusual about unions that you would
not expect that code?

Besides the issue of

1. assignment of a union to a union

you have not addressed the issues of

2. union inizialization using a union variable on the RHS in C99

3. union initialization using a union initializer

I expect that these issues can be rather subtle, and they're not treated
very well in most textbooks since unions are considered a little out of
style, but they are nevertheless useful in many situations.

Best Regards,

Neil
 
J

Jack Klein

But the above quote from 3.3.2.3 from the C89 rationale does not state whether
the same applies to unions (besides structures). Does it?

The rationale is a very handy document indeed when one wants insight
on the reasons behind some of the design features of the C language,
but it is not normative. Have you looked at the C89 standard itself?
Furthermore, it does not mention the ins and outs of initializing a union at
declaration time rather than through an assignment, either via another union
variable or via an initializer list. Is this at all legal in C89? How about
in C99?



You claim this is true, but the above quote of the C89 rationale by Pieter
only talks about structures, not unions, so I am not entirely convinced.

It makes no difference what inference you choose to draw from the
rationale, or for that matter the exact wording of the rationale
itself. The rationale is not normative and is not the standard. The
standard is the standard, it is definitive and authoritative, barring
the inevitable human errors.

As for the ANSI89/ISO90 standard, the first paragraph of 6.2.2.1
states, in part, "A modifiable lvalue is an lvalue that does not have
array type, does not have an incomplete type, does not have a
const-qualified type. and if it is a structure or union. does not have
any member (including. recursively, any member of all contained
structures or unions) with a const-qualified type."

This specifically includes unions in the definition of modifiable
lvalues, and any modifiable lvalue can be assigned to with an rvalue
of appropriate type.

The very next sentence, the first sentence in the second paragraph,
states: "Except when it is the operand of the sizeof operator, the
unary & operator, the ++ operator, the -- operator, or the left
operand of the . operator or an assignment operator, an lvalue that
does not have array type is converted to the value stored in the
designated object (and is no longer an lvalue)."

So when you initialize a union with another union of compatible type,
or when you assign to a union from another union of compatible type,
the value of the source object is converted to an rvalue, and that
rvalue is stored in the destination object. This includes all
sub-objects ad infinitum.
And what if the union contains other structures and unions. Does assignment
of compatible unions still work in such cases, according to the C standard?


Any book that mentions unions states the above: that's what a union is.


This is what I was hoping for, but does the standard even mention this?


Besides the issue of

1. assignment of a union to a union

you have not addressed the issues of

2. union inizialization using a union variable on the RHS in C99

This is not a C99 issue. As I said in another post in another one of
your threads, there are certain differences between initialization and
assignment. One of the largest differences deals with non-modifiable
lvalues, meaning objects of array type or const qualified as above.
They may be initialized, not assigned. Another deals with static
storage duration modifiable lvalues at file scope, which can only be
initialized with compile-time constant expressions, but can be
assigned with different types of expressions by executing code.
3. union initialization using a union initializer

I expect that these issues can be rather subtle, and they're not treated
very well in most textbooks since unions are considered a little out of
style, but they are nevertheless useful in many situations.

Best Regards,

Neil

First, I don't have to address anything. I asserted something about
both the initialization and assignment of unions, and in almost 24
hours none of the other regulars has pounced on any error and ripped
me to shreds. I guarantee you this would have happened had I been
wrong.

What reason do you have to dispute my reply? You have no quote from
the standard that contradicts me, nor from any other poster to this
group.

Still, just this once, keeping to the C90 ISO standard...

=======
6.3.16 Assignment operators
[...]
Constraints
An assignment operator shall have a modifiable lvalue as its left
operand.
Semantics
An assignment operator stores a value in the object designated by the
left operand.
[...]
6.3.16.1 Simple assignment
Constraints
One of the following shall hold:
[...]
- the left operand has a qualified or unqualified version of a
structure or union type compatible with the type of the right;
========

So the ANSI89/ISO90 standard directly states that unions, as well as
structures, may be directly assigned.

========
6.57 Initialization
[...]
A brace-enclosed initializer for a union object initializes the member
that appears first in the declaration list of the union type.

The initializer for a structure or union object that has automatic
storage duration either shall be an initializer list as described
below, or shall be a single expression that has compatible
structure or union type. In the latter case. the initial value of the
object is that of the expression.
========

So the ANSI89/ISO90 standard directly and clearly states that a union
(with automatic storage duration) may be initialized from another
union of compatible type.

I leave it as an exercise to you to, since you seem to have a copy of
C99, either:

1. Find the corresponding wording in C99 to prove that both
assignment to and initialization of unions is still defined to be
exactly the same, other than the new designated initializers, or...

2. Some wording in C99 that changes this behavior.

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

pete

Jack said:
Have you looked at the C89 standard itself?

I downloaded a public last draft from Dan Pop once
but I lost the URL.
Somebody else, I think the name was Bobbits,
used to have a copy on the web, but it's not there anymore.

Do you know a URL for a C89 public last draft?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top