LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   what's this? (unsigned int) & 'something' (https://www.linuxquestions.org/questions/programming-9/whats-this-unsigned-int-and-something-841458/)

lesca 10-30-2010 08:19 PM

what's this? (unsigned int) & 'something'
 
Hello everyone

When I 'call' offsetof(3) from <stddef.h>, I find 'offsetof' is actually a macro.

I expand it with Eclipse (with CDT), the expanded form is:
Code:

((size_t) &((Node *)0)->next)
Wherein, the Node is a type I defined:
Code:

typedef struct NODE
{
        int value;
        struct NODE * next;
} Node ;

And I know size_t is actually unsigned int in my computer.
So the fully expanded form is:
Code:

( (unsigned int) & ((Node *)0)->next )
We offten evaluate values like " 0x000f & var ", so that we can get the designated bits we want.
But here, the AND opreator seems doesn't work in that way.
And I find my questions:
1. How does it work?
2. On the left side of AND, that is a built-in type. On the right side, what is it?

Thanks!

paulsm4 10-30-2010 10:33 PM

Hi -

The "&" operator has two completely different meanings in C/C++:

a) boolean "and" operator
b) "address of" operator

This is an example of "address of":
Code:

((size_t) &((Node *)0)->next)
Here's a good link:
http://www.cprogramming.com/tutorial/lesson6.html

lesca 10-30-2010 11:48 PM

Aha! Thanks!

I know pointer, I just couldn't associate this expression with that "address of" operator.

"&((Node *)0)->next" gets the address of next from the beginning of the memory, and then cast the value of address to size_t.

I figure it out!

Thanks very much for your tips!

TobiSGD 10-31-2010 12:17 AM

Please mark your thread as solved with the thread tools if you found a solution.


All times are GMT -5. The time now is 08:45 AM.