 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
lallous Guest
|
Posted: Sun Jun 29, 2003 7:45 pm Post subject: DistanceInStruct macro. |
|
|
I have the following:
struct struc1
{
int a;
int b;
int c;
};
#define DistanceInStruct(member, struc) (unsigned long)((unsigned
long)(&struc.member) - (unsigned long)(&struc))
I can to this:
int main()
{
struc1 A;
int pos = DistanceInStruct(b, A); // returns 1 * sizeof(int)
pos = DistanceInStruct(c, A); // returns 2 * sizeof(int)
}
how can i accomplish the same but w/o having to declare a local
variable "A" of type 'struc1' ?
Regards,
Elias
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Sun Jun 29, 2003 7:55 pm Post subject: Re: DistanceInStruct macro. |
|
|
"lallous" <lallous (AT) lgwm (DOT) org> wrote
| Quote: | I have the following:
struct struc1
{
int a;
int b;
int c;
};
#define DistanceInStruct(member, struc) (unsigned long)((unsigned
long)(&struc.member) - (unsigned long)(&struc))
I can to this:
int main()
{
struc1 A;
int pos = DistanceInStruct(b, A); // returns 1 * sizeof(int)
pos = DistanceInStruct(c, A); // returns 2 * sizeof(int)
}
how can i accomplish the same but w/o having to declare a local
variable "A" of type 'struc1' ?
Regards,
Elias
|
Use the offsetof macro, defined in <stddef.h>
int pos = offsetof(struc1, b);
john
|
|
| Back to top |
|
 |
lallous Guest
|
Posted: Mon Jun 30, 2003 10:22 am Post subject: Re: DistanceInStruct macro. |
|
|
Hi John,
Thanks that what I was looking for.
Elias
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|