Something new for C?

Michael J. Andrew mick at auspyr.UUCP
Tue Nov 15 11:31:16 AEST 1988


In article <73 at dsoft.UUCP> root at dsoft.UUCP (Super user) writes:
|Maybe I've missed something in C, but One of the things I've never found a way
|to do and have always wanted, was a precompiler command to allow me to use
|the offset of an item into a structure. for example:
|
|struct test {
|   long this;
|   int  that;
|   char those[8];
|};
|
|   val = offset(test,that);

Try the following:

#define OFFSET(structure,field)  ((int) &(((struct structure *)0)->field))

A useful trick which I'm afraid I cannot take credit for. Watch out for
hardware with brain damaged (read "clever") addressing!
-- 
-------------------------------------------------------------------------
Michael Andrew        Sr. Systems Engineer    Austec, Inc.  San Jose CA.
mick at auspyr.UUCP  or amdahl!auspyr!mick       (408) 279 5533



More information about the Comp.lang.c mailing list