Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rochester!pt.cs.cmu.edu!spice.cs.cmu.edu!bader From: bader@spice.cs.cmu.edu (Miles Bader) Newsgroups: comp.lang.c Subject: structure element offsets Message-ID: <1096@spice.cs.cmu.edu> Date: Mon, 24-Nov-86 02:53:05 EST Article-I.D.: spice.1096 Posted: Mon Nov 24 02:53:05 1986 Date-Received: Mon, 24-Nov-86 22:00:33 EST Organization: Carnegie-Mellon University, CS/RI Lines: 29 Keywords: structure, offset Is there any way of finding the offset of a structure element from the beginning of a structure in a portable AND efficient way? I have a structure that looks like this: struct fentry{ FPOOL *pool; union{ FENTRY *next; char buf[1]; /* extended as needed ... */ } data; }; And I want to find the base of the structure given address of buf. My first one went like: static fentry calc; #define FALLOC_OFFSET ((long)calc.data.buf-(long)&calc) This worked, and with pcc on an sun, compiled into the correct constant, 4. However on an ibm rt (under both pcc and another non-pcc compiler), it did the calculation at run time (despite the fact that the two quantities being subtracted were the same address with offsets differing by 4). I have given up and defined this as the constant 4 (which it is on any machine around here), but am just curious if there's a better way. -Miles