about address...

S

s88

Hi all:
I saw a code slice
<code>
&(((structure *)0)->m)
</code>
Looks like to identified the address of the element in the structure.
Could U explain that the meanning of the 0???

Thanx!!
 
E

Emmanuel Delahaye

s88 a écrit :
I saw a code slice
<code>
&(((structure *)0)->m)
</code>
Looks like to identified the address of the element in the structure.
Could U explain that the meanning of the 0???

This is a complicated way of writing

<code>
#include <stddef.h>

<...> offsetof(structure, m);

</code>

Do you really need to know about the gory details ?
 
P

pemo

s88 said:
Hi all:
I saw a code slice
<code>
&(((structure *)0)->m)
</code>
Looks like to identified the address of the element in the structure.
Could U explain that the meanning of the 0???

Zero is being cast to a 'structure *' - it'd interesting to see structure,
and what 'm' is.
 
M

Mockey Chen

Does this idiom portable?
I means compile ok and run correctly on every platform and compiler.
 
E

Emmanuel Delahaye

Mockey Chen a écrit :

[Please don't top-post. Fixed]
> Is this idiom portable?
> I means compile ok and run correctly on every platform and compiler.

Yes it has belonged to the C-language since 1989.
 
K

Keith Thompson

Mockey Chen said:
Does this idiom portable?
I means compile ok and run correctly on every platform and compiler.

Every conforming C implementation supports the offsetof() macro.

The code slice above is not portable. It's one possible
implementation of offsetof().
 

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
474,171
Messages
2,570,936
Members
47,472
Latest member
KarissaBor

Latest Threads

Top