Printing offset of a member of structure

J

junky_fellow

I am trying to print the offset of a particulat member in
a structure, but it's not working. I am using the following
expression to print the offset.

&(struct my_struct *)0->member_name

What's wrong with this ?

If I use,
&((struct my_struct *)0)->member_name, it works. But it seems
to be wrong. It looks as if I am trying to print the address of
structure itself instead of the member.


Thanx in advance for any help ....
 
K

Keith Thompson

I am trying to print the offset of a particulat member in
a structure, but it's not working.

I am using the following
expression to print the offset.

&(struct my_struct *)0->member_name

What's wrong with this ?

The -> operator binds more tightly than a cast, so you're trying to
cast the result of 0->member_name.
If I use,
&((struct my_struct *)0)->member_name, it works. But it seems
to be wrong. It looks as if I am trying to print the address of
structure itself instead of the member.

One possible definition of offsetof() is:

#define offsetof(s, m) (size_t)(&(((s *)0)->m))

(s *)0 is a pointer to a struct at address zero.

((s *)0)->m is a member of that structure.

If the member is at offset 6 within the structure, that member of
the structure at address 0 will have an address of 6.

Converting that address to size_t yields the value 6, which is what
you want.

*All* of this is extremely system-specific. It depends on numerous
assumptions about how address are represented, conversions from
addresses to size_t, and so forth. You cannot make assumptions in
portable code -- but the implementer of the runtime library is
entitled to make whatever assumptions are supported by the compiler.

That's why the offsetof() macro is defined in the standard library;
there's no portable way to implement it, but there's always a
non-portable way that works for a given implementation. The implementer
is allowed to do things that you aren't.
 
R

Richard Bos

I am trying to print the offset of a particulat member in
a structure, but it's not working. I am using the following
expression to print the offset.

&(struct my_struct *)0->member_name

What's wrong with this ?

It doesn't use the macro the Standard provides for this very purpose,
that's what's wrong with it. Also, you have failed to read the FAQ.
Again.

Richard
 
C

CBFalconer

I am trying to print the offset of a particulat member in
a structure, but it's not working. I am using the following
expression to print the offset.

&(struct my_struct *)0->member_name

What's wrong with this ?

It has nothing to do with reality. However there is a standard
macro, named offsetof, to satisfy your urge.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
R

Rajan

This was what I did to get the offset:
#define OFF(M,S) ((size_t)&(((M *)0)->S))

typedef struct{
int i, j;
char c;
}gen;

I wanted to print offset of c;

size_t size = OFF(gen,c);
printf("%d\n",size);
Try this out.

BTW, I did'nt know there is offsetof which Keith pointed out.

Do tell me if it works
 
L

Lawrence Kirby

I am trying to print the offset of a particulat member in
a structure, but it's not working. I am using the following
expression to print the offset.

&(struct my_struct *)0->member_name

Standard C providea a macro to do this called offsetof() which is defined
in the standard header said:
What's wrong with this ?

-> has higher precedence than a cast so it is parsed as
&(struct mystruct *) (0->member_name)
If I use,
&((struct my_struct *)0)->member_name, it works. But it seems to be
wrong. It looks as if I am trying to print the address of structure
itself instead of the member.

Ys it is generating the address of a structure element for a notional
structure at address 0. You need to convert thjis to an integer to get an
offsdet value. Of course this is non-portable. There is no way to write
portable code to do this directly, so C defines a macro provided by each
implementation which works on that implementations.

Lawrence
 
J

junky_fellow

Keith said:
The -> operator binds more tightly than a cast, so you're trying to
cast the result of 0->member_name.

Thanx a lot for the explanation. Actually, I was confused by the fact
that ()
operator has higher precedence than -> operator.
But, here () have been used as a part of cast operator.
 
L

Lawrence Kirby

On Tue, 21 Jun 2005 04:26:13 -0700, junky_fellow wrote:

....
Thanx a lot for the explanation. Actually, I was confused by the fact
that ()
operator has higher precedence than -> operator.

The () in precedence tables refers to the function call operator
which has the same precedence as ->. If it was specified with higher
precedence then for example

a.b();

would be parsed incorrectly.

Lawrence
 
J

Jack Klein

This was what I did to get the offset:
#define OFF(M,S) ((size_t)&(((M *)0)->S))

Dereferencing a null pointer is undefined behavior.
typedef struct{
int i, j;
char c;
}gen;

I wanted to print offset of c;

size_t size = OFF(gen,c);
printf("%d\n",size);

Passing an argument of type size_t to printf() with a "%d" conversion
specifier produces undefined behavior.
Try this out.

BTW, I did'nt know there is offsetof which Keith pointed out.

Have you tried looking it up in your C reference book or copy of the
standard? Or in your compiler's documentation, online help, or man
pages?
Do tell me if it works

Did you type:

offsetof macro

....into the search box at google.com and look at the first few
answers? You don't even need to open the linked articles, several of
them answer your question in the small excerpt on the results page.

Did you read question 2.14 in the FAQ for this newsgroup (link in my
signature)? And its answer, of course.

Note that the offsetof macro produces a value of type size_t, you need
to cast it to int to pass to printf() with a "%d" conversion
specifier.
 
R

Rajan

Jack,
I tried doing this and it gives me absolutely no problems printing the
offset.
Check this out.
 
K

Keith Thompson

Rajan said:
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.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top