union or type casting, which to use?
Paul D. Smith
pds at lemming.webo.dg.com
Sat Dec 1 01:45:46 AEST 1990
[] In article <PDS.90Nov27180241 at lemming.webo.dg.com>
[] pds at lemming.webo.dg.com (Paul D. Smith) writes:
[] >In your case, you have no problem: the union elements are both the
[] >same size and are guaranteed by ANSI to be compatible -->with
[] >a cast<--:
[] >both are pointers. [details deleted, see original article]
[] Are you certain? [proof deleted, see original article]
Hmm. I see your point here. I retract my statement about "guaranteed
by ANSI". It would appear that there really is no truly portable way
to initialize unions of any reasonable complexity; if you don't want
to use the first element of the union then you are SOL (unless you are
making a union where all elements have the same type structure, which
could be useful in some applications).
Of course, I'd venture to say that the static initialization I posted
would work on 99% of machines available today; does *anyone* know of a
machine which uses different internal representations of pointers for
different structure types? Also of course, it's always that last 1%
which is the kick in the pants :-)
Bummer. Too bad ANSI couldn't come up with some more syntax for
initializing unions ... (yes I know it would be difficult and not
very compatible, and no I don't have any good ideas offhand, and I'm
not criticizing them, just thinking wistfully ... )
--
paul
-----
------------------------------------------------------------------
| Paul D. Smith | pds at lemming.webo.dg.com |
| Data General Corp. | |
| Network Services Development Division | "Pretty Damn S..." |
| Open Network Systems Development | |
------------------------------------------------------------------
More information about the Comp.lang.c
mailing list