OFFSET of Structure Member

R

RAKHE

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
 
M

Mark Bluemel

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?
 
K

Keith Thompson

RAKHE said:
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?
 
U

Uno

pete said:
/* 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?
 
P

Peter Nilsson

[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?
 
U

Uno

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

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top