accessing initial common structure members
Chuck Kozak
kozak at grad2.cis.upenn.edu
Sun Oct 21 10:08:43 AEST 1990
I have seen code where the first N members of different structures
are accessed by code that treats the structures as identical. I
looked through K&R II to check whether this practice is legal and
cannot find any mention of it. This practice works with GNU C on a
SUN-4 and I cannot think of a reason why it would not work on other
architectures, but I would like to know whether it is a safe or bad
practice.
An example of what I mean is given below.
struct common {
struct common *next;
struct common *prev;
int type;
};
struct type_1 {
struct common *next;
struct common *prev;
int type;
... type_1 specific members ...
};
struct type_2 {
struct common *next;
struct common *prev;
int type;
... type_2 specific members ...
};
void common_function(struct common *pnt)
{
... access the next, prev, and type members ...
}
... other code ...
struct type_1 type_1_data;
common_function((struct common *) &type_1_data);
...
thanks for your help,
Chuck Kozak
kozak at grad2.cis.upenn.edu
More information about the Comp.lang.c
mailing list