• C

macro to find the byte offset of a struct member

Can someone please let me know how to write a macro that would find the byte
offset of a structure member...

Regards & Thanks,
arut
arutAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

scrapdogCommented:
#define BYTEOFFSET(x,y) &(x.y)-&(x)

where x is the structure and y is the member

you may also need to define a macro for pointers to structures, such as

#define PBYTEOFFSET(x,y) &(x->y)-x
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
scrapdogCommented:
note that in both cases x would have to be a structure variable
0
KangaRooCommented:
There's a standard ANSI C macro for that:

#include <stddef.h>
size_t offsetof(struct_type, struct_member);
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

KangaRooCommented:
#define      offsetof(TYPE, MEMBER)      ((size_t) &( ((TYPE *) 0)->MEMBER ))
0
arutAuthor Commented:
Dear ScrapDog,

                      Your solution does not seem to work.
                       error is : invalid operands to binary '-' operator.

                       Could you please provide a corrected code.

Regards & THanks,
arut


Dear Kangaroo,

                     Could you please explain what
                  ((Type *)0)->member is trying to do.

                 Why 0?




Regards & Thanks,
arut
0
scrapdogCommented:
d'oh!  sorry, I probably should have tested it...it looks like you need to typecast after all (as in KangaRoo's answer).

#define BYTEOFFSET(x,y) ((long int)&(x.y))-((long int)&(x))
0
arutAuthor Commented:
I'll try this one out!

Thank you,
arut
0
KangaRooCommented:
 ((TYPE*)0)
Cast NULL pointer to a pointer to TYPE (TYPE*)
  ((TYPE*)0)->MEMBER
Returns TYPE.MEMBER, next step takes the address of that member.
  &(((TYPE*)0)->MEMBER)
Returns the address of the MEMBER. Since 0 was used as the address for the TYPE object, this address is also the offset to the member.
Would be the similar to:
  TYPE tmp;
  size_t offset = &(tmp.MEMBER) - &tmp;
with &tmp == 0;
0
KangaRooCommented:
TW, this method does not require an object...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.