Category : C++ Source Code
Archive   : C_ALL.ZIP
Filename : TI655.ASC

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







PRODUCT : Borland C++ NUMBER : 655
VERSION : All
OS : PC DOS
DATE : August 20, 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

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.

DISCLAIMER: You have the right to use this technical information
subject to the terms of the No-Nonsense License Statement that
you received with the Borland product to which this information
pertains.
























  3 Responses to “Category : C++ Source Code
Archive   : C_ALL.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: http://www.os2museum.com/wp/mtswslnk/