DistanceInStruct macro.

L

lallous

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
 
J

John Harrison

lallous said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top