Union element alignment query

Stephen Clamage steve at taumet.com
Sun Nov 18 10:59:19 AEST 1990


curry at hplred.HP.COM (Bo Curry) writes:

!union {
!   FOOTYPE	a[4];
!   BARTYPE        b;
!} combo;

!Does the standard guarantee that (void *)(&combo.a[0]) == (void *)(&combo.b) ?

Not exactly.  The standard (section 3.5.2.1) requires that
	(FOOTYPE*)&combo == &combo.a[0]
and that
	(BARTYPE*)&combo == &combo.b
There is also a requirement that given a type T, and the declaration
	T* t;
then
	(T*)(void*)t == t;
I don't believe that there is any requirement that the relation you specify
must hold.  On machines where pointers to different types may have
different sizes, their conversions to void* might not be equal.  The
standard only requires that conversion from void* back to the original
pointer type must work.
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list