Mikhail said:
Thanks for the most useful answer... Any ideas on why this is not part of
the C-standard (yet?)? What's wrong with this extension -- what does it
break? Yours,
There are no clear, standardized rules.
Example:
What happens for
union foo {
float bar;
long double baz;
void *qux;
int *quux;
};
if you perform
union foo Foo = 0;
As a cast is only an explicit conversion, this is the same
as
union foo Foo = (union foo) 0;
Do we get the representation of 0.0F, 0.0L, (void *)0, or (int *)0?
Note: the latter two may in theory differ.
In C99, you have compound literals to work around that (note:
compound literals look like casts but are not casts -- they
yield lvalues):
,---
#include <stdio.h>
enum which {
eWhichInvalid = -1,
eWhichFlt,
eWhichLdbl,
eWhichVPtr,
eWhichIPtr
};
union foo {
float bar;
long double baz;
void *qux;
int *quux;
};
void blib (enum which Which, union foo Foo)
{
switch (Which)
{
case eWhichFlt:
printf("%g\n", Foo.bar);
break;
case eWhichIPtr:
printf("%p\n", (void *) Foo.quux);
break;
default:
puts("Too lazy for more");
break;
}
}
int main (void)
{
blib(eWhichFlt, (union foo){.bar = 0});
blib(eWhichIPtr, (union foo){.quux = 0});
return 0;
}
`---
Cheers
Michael