wrote:
Please leave the attribution in so we can see who said what.
Attributions being the lines like the above saying things like, "fred
wrote".
>>> If no, please make a suggestion.
>> I'm not sure why one would do this - if you have a definition of foo
>> why not use it?
>
> I think the reason is because you may not have a definition. Suppose
> you pass a pointer around to code that does not care what's in offset.
> That code would only need to access member d. It may be receiving
> pointers to structs of various definitions that it does not know about.
> Nor could it konw if the definition can change.
If you are doing that, then there is a special dispensation for the
*initial* members of a struct when they appear in a union. I.e.
struct foo {
int type;
char name[NAME_SIZE];
double val;
};
struct bar {
int type;
char name[NAME_SIZE];
int val;
}
union foobar {
struct foo foomess;
struct bar barmess;
}
In the above, you are allowed to use either foo or bar to access the
type and name fields, but from val on you have to use the correct struct
type. So for the type of thing you are discussing the correct method is
to put the common elements at the start of the struct and the elements
that vary at the end. It may simplify getting it right to put all the
initial members in to a struct and doing something like:
struct head {
int type;
char name[NAME_SIZE];
};
struct foo {
struct head messhead;
double val;
};
struct bar {
struct head messhead;
int val;
};
This also avoids the technical requirement to have the structs in a
union because you are allowed to convert a pointer to a struct to a
pointer to its first element, which is now itself a struct containing
all of the common elements. This is how I am generally inclined to do it.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.