Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > OFFSET of Structure Member

Reply
Thread Tools

OFFSET of Structure Member

 
 
RAKHE
Guest
Posts: n/a
 
      06-24-2010
struct emp
{
int a;
char b;
int c;
};



I want to find offset of the structure member a, b, c, please anyone
explain about this






 
Reply With Quote
 
 
 
 
Alexander Klauer
Guest
Posts: n/a
 
      06-24-2010
RAKHE wrote:
> struct emp
> {
> int a;
> char b;
> int c;
> };
>
> I want to find offset of the structure member a, b, c, please anyone
> explain about this


http://c-faq.com/struct/offsetof.html
 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      06-24-2010
On 24 June, 11:07, RAKHE <(E-Mail Removed)> wrote:
> struct emp
> * * * * {
> * * * * * * * * int a;
> * * * * * * * * char b;
> * * * * * * * * int c;
> * * * * };
>
> I want to find offset of the structure member a, b, c, please anyone
> explain about this


What do you need explained?
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-24-2010
RAKHE <(E-Mail Removed)> writes:
> struct emp
> {
> int a;
> char b;
> int c;
> };
>
>
>
> I want to find offset of the structure member a, b, c, please anyone
> explain about this


Why? If you have the offset, what do you intend to use it for?

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Uno
Guest
Posts: n/a
 
      06-25-2010
pete wrote:

> /* BEGIN new.c */
>
> #include <stdio.h>
> #include <stddef.h>
>
> int main(void)
> {
> struct emp {
> int a;
> char b;
> int c;
> };
>
> printf("offsetof(struct emp, a) is %lu\n",
> (long unsigned) offsetof(struct emp, a));
>
> printf("offsetof(struct emp, b) is %lu\n",
> (long unsigned) offsetof(struct emp, b));
>
> printf("offsetof(struct emp, c) is %lu\n",
> (long unsigned) offsetof(struct emp, c));
>
> return 0;
> }
>
> /* END new.c */
>


$ gcc -Wall -Wextra p2.c -o out
$ ./out
offsetof(struct emp, a) is 0
offsetof(struct emp, b) is 4
offsetof(struct emp, c) is 8
$

In this case, my implementation's macro is essentially the same as
Plauger's.

Linux:
#ifdef __compiler_offsetof
#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

Plauger:
#define offsetof(T, member) ((_Sizet)&((T *)0)->member)

Can you say a few words about how it works?
--
Uno
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      06-25-2010
Uno <(E-Mail Removed)> wrote:
> Linux:
> ...
> #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
> ...
> Plauger:
> #define offsetof(T, member) * * ((_Sizet)&((T *)0)->member)
>
> Can you say a few words about how it works?


<http://c-faq.com/struct/offsetof.html>

The two implementations above assume null pointers map to
'address zero'.

--
Peter
 
Reply With Quote
 
Uno
Guest
Posts: n/a
 
      06-25-2010
Peter Nilsson wrote:
> Uno <(E-Mail Removed)> wrote:
>> Linux:
>> ...
>> #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)


[snipping Plauger.]
[Please refer to the above macro herewith.]
>> Can you say a few words about how it works?

>
> <http://c-faq.com/struct/offsetof.html>
>
> The two implementations above assume null pointers map to
> 'address zero'.


Can you "talk through" the whole thing? It takes me a bit with the logic.
--
UNo
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      06-25-2010
On Jun 25, 2:22*pm, Uno <(E-Mail Removed)> wrote:
> Peter Nilsson wrote:
> > Uno <(E-Mail Removed)> wrote:
> >> Linux:
> >> ...
> >> #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

>
> [snipping Plauger.]
> [Please refer to the above macro herewith.]
>
> Can you "talk through" the whole thing?


Suppose you have...

struct {
T1 elm1;
T2 elm2;
T3 elm3;
};

An instance will be layed out as...

[elm1][elm2][elm3]
^ ^ ^
| | |
addr1 addr2 addr3

....where addr2 and addr3 will be a fixed number of bytes
(offset) from addr1.

Now suppose you could lay out the same instance at
address 0...

[elm1][elm2][elm3]
^ ^ ^
| | |
0 addr2 addr3

At a guess, what do you suppose addr2 and addr3 will
represent in the second case?

--
Peter
 
Reply With Quote
 
Uno
Guest
Posts: n/a
 
      06-29-2010
On 6/24/2010 10:25 PM, Peter Nilsson wrote:
> On Jun 25, 2:22 pm, Uno<(E-Mail Removed)> wrote:
>> Peter Nilsson wrote:
>>> Uno<(E-Mail Removed)> wrote:
>>>> Linux:
>>>> ...
>>>> #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)

>>
>> [snipping Plauger.]
>> [Please refer to the above macro herewith.]
>>
>> Can you "talk through" the whole thing?

>
> Suppose you have...
>
> struct {
> T1 elm1;
> T2 elm2;
> T3 elm3;
> };
>
> An instance will be layed out as...
>
> [elm1][elm2][elm3]
> ^ ^ ^
> | | |
> addr1 addr2 addr3
>
> ...where addr2 and addr3 will be a fixed number of bytes
> (offset) from addr1.
>
> Now suppose you could lay out the same instance at
> address 0...
>
> [elm1][elm2][elm3]
> ^ ^ ^
> | | |
> 0 addr2 addr3
>
> At a guess, what do you suppose addr2 and addr3 will
> represent in the second case?


They are where struct.elm2 and struct.elm3 respectively begin.
--
Uno
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing offset-aware and offset-naive datetimes? Roy Smith Python 4 01-27-2013 03:17 PM
Structure member offset. gokrix C Programming 10 12-02-2005 10:24 PM
Printing offset of a member of structure junky_fellow@yahoo.co.in C Programming 10 06-23-2005 12:31 PM
offset of a member inside a structure junky_fellow C Programming 15 10-24-2004 05:48 AM
Translated Offset to Source Offset Lance Riedel XML 2 10-15-2003 03:04 PM



Advertisments