Unions Redux
- From: "Old Wolf" <oldwolf@xxxxxxxxxxxxxx>
- Date: 14 Mar 2007 15:10:44 -0700
Ok, we've had two long and haphazard threads about unions recently,
and I still don't feel any closer to certainty about what is permitted
and what isn't. The other thread topics were "Real Life Unions"
and "union {unsigned char u[10]; ...} ".
Here's a concrete example:
#include <stdio.h>
int main(void)
{
union { int s; unsigned int us; } u;
u.us = 50;
printf("%d\n", u.s);
return 0;
}
Is this program well-defined (printing 50), implementation-defined, or
UB ?
Note that the aliasing rules in C99 6.5 are not violated here -- it is
not forbidden under that section to access an object of some type T
with an lvalue expression whose type is the signed or unsigned
version of T.
In other words, is there anything other than the aliasing rules that
restrict 'free' use of unions?
.
- Follow-Ups:
- Re: Unions Redux
- From: Robert Gamble
- Re: Unions Redux
- From: Jack Klein
- Re: Unions Redux
- From: Pierre Asselin
- Re: Unions Redux
- Prev by Date: Re: Function call with wrong number of parameters
- Next by Date: Re: C89, size_t, and long
- Previous by thread: Function call with wrong number of parameters
- Next by thread: Re: Unions Redux
- Index(es):
Relevant Pages
|