(Really addressing inside struct)
gwyn at brl-smoke.UUCP
gwyn at brl-smoke.UUCP
Sat Jul 12 23:14:59 AEST 1986
In article <403 at anasazi.UUCP> john at anasazi.UUCP (John Moore) writes:
-struct links {
- struct links *forward;
- struct links *backward;
-};
-
-struct foo {
- char bar;
- struct links snake;
- struct whocares junk;
- struct links lizard;
-};
-
-struct links *reptile;
-
-Assume that reptile points to the lizard structure of a "foo" structure.
-What construct allows one to do the following:
-
-struct foo *iwish = (mystery function of reptile);
-
-Example of a wrong solution:
-struct foo *iwish = (struct foo *)((char *)reptile + (char *)0->lizard);
This is rather a strange way to do business, but I can answer the question:
There is no guaranteed way to do what you're trying to do, therefore there
should be no way to keep "lint" from complaining. However, the following
should work for most C implementations:
struct foo dummy;
#define LIZ_OFFSET ((char *)&dummy.lizard - (char *)&dummy.bar)
struct foo *iwish = (struct foo *)((char *)reptile - LIZ_OFFSET);
What I would recommend instead is that you keep a pointer to the whole
(struct foo) rather than to the lizard member, if your application permits.
(You might need to add a (struct foo *) member to (struct links).)
More information about the Comp.lang.c
mailing list