home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Black Box 4
/
BlackBox.cdr
/
progc
/
c_all592.arj
/
TI655.ASC
< prev
next >
Wrap
Text File
|
1992-02-25
|
1KB
|
67 lines
PRODUCT : Borland C NUMBER : 655
VERSION : All
OS : PC DOS
DATE : February 25, 1992 PAGE : 1/1
TITLE : Getting the Offset of a Member of a Structure
How to get the offset of a member of a structure:
#include <stddef.h>
struct mys {
int member1;
int member2;
};
int where_b = offsetof( struct mys , member2 );
What this looks like is this:
int where_b = (size_t)&(((struct mys _FAR *)0)->member2);
What this does is typecast absolute address 0 to be a far pointer
to the struct. Then it references member2 off of this address.
Then we take the address of this location. Because 0 is cast to a
far pointer, we are starting at 0000:0000 so the address of
member2 will be the relative offset of a member2 within any
structure. All this can be evaluated at compile time.