C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

DistanceInStruct macro.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
lallous
Guest





PostPosted: Sun Jun 29, 2003 7:45 pm    Post subject: DistanceInStruct macro. Reply with 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
Back to top
John Harrison
Guest





PostPosted: Sun Jun 29, 2003 7:55 pm    Post subject: Re: DistanceInStruct macro. Reply with quote




"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





PostPosted: Mon Jun 30, 2003 10:22 am    Post subject: Re: DistanceInStruct macro. Reply with quote



Hi John,

Thanks that what I was looking for.

Elias
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.