Shared initial struct elements... history provides an answer?
Kevin Martin
kpmartin at watmath.UUCP
Mon Jan 14 13:53:49 AEST 1985
The problem:
struct shared {
int discriminant;
type1 shared1;
type2 shared2;
};
struct version1 {
struct shared _x_;
type3 unshared1;
type4 unshared2;
};
/* etc. */
The problem is this funny _x_ that must be introduced in the names of the
shared parts.
The history:
Early C compilers were somewhat lax about structures, and you could say
something like
struct version1 {
struct {
int discriminant;
type1 shared1;
type2 shared2;
}; /* note: no _x_ */
type3 unshared1;
type3 unshared2;
};
And everything worked.
This disappeared about the same time typed struture pointers appeared.
The solution:
In a structure or union declaration, if an element declaration of
type struct or union is given *with no declarators*, the elements
of the inner struct or union are re-declared (additionally declared?)
as elements of the outer struct or union, with their offsets being the
offset of the start of the inner struct/union plus their offset within
that struct or union. (phew!).
Note that the inner elements would have to be unique in the outer struct/
union.
The solved example:
struct shared {
int discriminant;
type1 shared1;
type2 shared2;
};
struct version1 {
struct shared;
type3 unshared1;
type4 unshared2;
};
/* etc. */
This change doesn't break existing code, and reduces the need for the
programmer to create extra names for those funny nested structs.
No, Henry, we haven't implemented it. We're waiting for the standard before
we make any more compiler changes. But I wanted to mention this hare-brained
scheme before someone decides to use another hare-brained scheme...
Kevin Martin, UofW Software Development Group
More information about the Comp.lang.c
mailing list