increment casted void pointer -- ANSI?

Blair P. Houghton bhoughto at pima.intel.com
Wed Mar 27 19:37:35 AEST 1991


In article <15589 at smoke.brl.mil> gwyn at smoke.brl.mil (Doug Gwyn) writes:
>In article <288 at nazgul.UUCP> bright at nazgul.UUCP (Walter Bright) writes:
>-In article <1991Mar8.165300.11692 at zoo.toronto.edu> henry at zoo.toronto.edu (Henry Spencer) writes:
>-/There is no way to say "pretend this variable has a different type" in C.
>-The usual to do a 'type paint', which is to change the type of an object
>
>A strictly conforming method is to use a union type.

Ouch!  My ears just pricked up so hard I think I pulled my skull...

Where in the std is it prohibited to fake unions by using a struct
and externally prohibiting attempts to dereference a value
that was superseded by one of a different type?

It seems to say only that a union has to be at least large enough
to hold it's member with the most bits, and to align members
suitably, but not that it has to overlap types.

The rationale parrots the "a pointer to a union, suitably
cast, points to each member".  Considering the latitude
given a cast, this may mean that the pointer could be
adjusted in byte-increments to point to the corresponding
type's member.  This implies strongly that overlap
must occur if there is more than one member with the
same type, but that doesn't buy too much "bit-paint".

I only saw the one paragraph, though (ANSI X3.159-1989,
sec. 3.5.2.1, p. 62, ll. 10-14), so I'd be glad if someone
came up with more stringent, explicit delineation of this
situation.

				--Blair
				  "Time to make the donut holes..."



More information about the Comp.std.c mailing list