468,785 Members | 1,536 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,785 developers. It's quick & easy.

unions: initialization and assignment


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

Nov 13 '05 #1
6 12916
On Wed, 17 Sep 2003 01:14:04 -0230, Neil Zanella <nz******@cs.mun.ca>
wrote in comp.lang.c:

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
Nov 13 '05 #2
On Wed, 17 Sep 2003 01:14:04 -0230
Neil Zanella <nz******@cs.mun.ca> wrote:

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

--
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
Nov 13 '05 #3
Pieter Droogendijk <gi*@binky.homeunix.org> wrote in message:
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 <ja*******@spamcop.net> wrote in message:
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
Nov 13 '05 #4
On 17 Sep 2003 17:10:54 -0700, nz******@cs.mun.ca (Neil Zanella) wrote
in comp.lang.c:
Pieter Droogendijk <gi*@binky.homeunix.org> wrote in message:
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?


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?

Jack Klein <ja*******@spamcop.net> wrote in message:
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.


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


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
Nov 13 '05 #5
Jack Klein wrote:
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?

--
pete
Nov 13 '05 #6
pete wrote:
Jack Klein wrote:
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?


http://danpop.home.cern.ch/danpop/ansi.c

Jirka

Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by john sun | last post: by
8 posts views Thread by Josh Lessard | last post: by
50 posts views Thread by Charles Stapleton | last post: by
7 posts views Thread by skishorev | last post: by
8 posts views Thread by lovecreatesbea... | last post: by
11 posts views Thread by asdf | last post: by
26 posts views Thread by Old Wolf | last post: by
2 posts views Thread by timlyee | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.