"Rajan" <rs*******@yahoo.com> writes:
Jack,
I tried doing this and it gives me absolutely no problems printing the
offset.
Check this out.
You tried what? Check what out? Please provide some context; don't
assume that everyone can easily see the article to which you're
replying.
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
In your previous article, you said you used the following to compute
the offset of a struct member:
#define OFF(M,S) ((size_t)&(((M *)0)->S))
As Jack explained, this invokes undefined behavior. Undefined
behavior includes working as you think it should. The OFF macro is
not-portable.
There is a perfectly portable way to compute the offset of a struct
member: the offsetof() macro defined in <stddef.h>.
The C FAQ is at <http://www.eskimo.com/~scs/C-faq/top.html>. Question
2.14 talks about the offsetof() macro. It also mentions an alternate
method to be used if the offsetof() macro is not available, but that's
largely obsolete; unless you're using a very old system, the
offsetof() macro *will* be available.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.