Need help with a programming question with $0.50 reward

Spamela

Diamond Member
Oct 30, 2000
3,859
0
76
it's calculating the offset in bytes of the element w of struct z.

i.e., the macro calculates that w is 4 + 8 = 12 bytes from the first byte of
a struct z (assuming no byte padding)

typedef struct
{
int a;
double b;
int w;
} z;
 

KingNothing

Diamond Member
Apr 6, 2002
7,141
1
0
Originally posted by: Spamela
it's calculating the offset in bytes of the element w of struct z.

i.e., the macro calculates that w is 4 + 8 = 12 bytes from the first byte of
a struct z (assuming no byte padding)

typedef struct
{
int a;
double b;
int w;
} z;

Can you post sample code illustrating this? I can't get his macro to compile.

BTW, dude8604, are you sure you typed the code exactly right?
 

Spamela

Diamond Member
Oct 30, 2000
3,859
0
76
Originally posted by: KingNothing
Originally posted by: Spamela
it's calculating the offset in bytes of the element w of struct z.

i.e., the macro calculates that w is 4 + 8 = 12 bytes from the first byte of
a struct z (assuming no byte padding)

typedef struct
{
int a;
double b;
int w;
} z;

Can you post sample code illustrating this? I can't get his macro to compile.

BTW, dude8604, are you sure you typed the code exactly right?

here goes (but i think the contest was won already).
i'd seen this #define before, but not named MAKE_COOL_GAMES

#pragma pack(1)

#define MAKE_COOL_GAMES(z, w) (int)(&(((z *)0)->w))

typedef struct
{
int a;
double b;
int c;
} Yow_t;


int _tmain(int argc, _TCHAR* argv[])
{
int iOffset = MAKE_COOL_GAMES(Yow_t, a);
printf ("Offset of a is %d\n", iOffset);


iOffset = MAKE_COOL_GAMES(Yow_t, b);
printf ("Offset of b is %d\n", iOffset);

iOffset = MAKE_COOL_GAMES(Yow_t, c);
printf ("Offset of c is %d\n", iOffset);

return 0;
}


 

KingNothing

Diamond Member
Apr 6, 2002
7,141
1
0
Sweet, I was just calling it correctly.

The null pointer threw me off too, I realized it was being created but wasn't sure why.