LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2010, 08:19 PM   #1
lesca
Member
 
Registered: Sep 2010
Posts: 58

Rep: Reputation: 0
Smile 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!

Last edited by lesca; 10-30-2010 at 08:20 PM.
 
Old 10-30-2010, 10:33 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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

Last edited by paulsm4; 10-30-2010 at 10:35 PM.
 
1 members found this post helpful.
Old 10-30-2010, 11:48 PM   #3
lesca
Member
 
Registered: Sep 2010
Posts: 58

Original Poster
Rep: Reputation: 0
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!
 
Old 10-31-2010, 12:17 AM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Please mark your thread as solved with the thread tools if you found a solution.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
unsigned long int uint32_t to unsigned char and back MicahCarrick Programming 2 08-02-2009 01:39 AM
what is the range of unsigned int shamsheer Linux - Newbie 6 04-07-2009 03:00 PM
invalid conversion from 'int' to 'unsigned int*' tigerhp Programming 2 03-02-2008 04:21 PM
print unsigned int alaios Programming 4 06-03-2005 09:34 AM
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration