Category : C Source Code
Archive   : BCPPTECH.ZIP
Filename : TI655.ASC

Output of file : TI655.ASC contained in archive : BCPPTECH.ZIP

PRODUCT : Borland C NUMBER : 655
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:


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.

  3 Responses to “Category : C Source Code
Archive   : BCPPTECH.ZIP
Filename : TI655.ASC

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: