[comp.lang.c] Portability of variable length structures

pcn (Phil Nelson) (03/09/88)

I would like to do the following:

enum types { Generic, Extended };

#define COMMON_FIELDS  enum types type; char *name

struct generic   { COMMON_FIELDS; };
struct extended  { COMMON_FIELDS; char *y; long z; };

At this point, I'd like to cast all instances of either the generic or
the extended structure to be of type (struct generic) in order to
reference the COMMON_FIELDS (type or name) ie

	struct extended *xxx;
	((struct generic *)xxx)->type 

I know this works on a Sun-3, and in MSC on a PC (w/o the structure
pack optimization). Is this a crime against nature? Does it violate
K&R and/or the new ANSI draft? What is the cleanest way to do this,
without resorting to a union which makes all structures as big as the
biggest? Does anyone know of a specific machine on which this is known
to fail? How about the Sun-4? Are there any tricks that makes this
at all cleaner (ie guaranteeing that all common fields are at least
the word size of the machine)? 

Any insights or experience will be appreciated.  Please e-mail
responses not of general interest. I will summarize if appropriate.


Phil Nelson
Advanced Decision Systems
Mtn View, Ca 94040

(pcn@ads.arpa or pcn@zodiac.ads.com from any arpanet site should do it)