offsetof

M

mihirtr

Hi,
I have following structure.

typedef struct{
char *abc;
dummy_struct *tmp;
char *xyz;
}my_struct;

I have pointer to tmp variable in this structure. Can I use offsetof()
function to access value stored in abc. Basically I want to get
pointer to my_struct. Is it possible using offsetof() function ?

Thanks,
 
K

karthikbalaguru

Hi,
I have following structure.

typedef struct{
char *abc;
dummy_struct *tmp;
char *xyz;

}my_struct;

I have pointer to tmp variable in this structure. Can I use offsetof()
function to access value stored in abc. Basically I want to get
pointer to my_struct. Is it possible using offsetof() function ?

Thanks,

Offsetof is for finding the offset of the particular member of the
structure from
its starting location in memory. It returns the number of bytes.
That is, It can be used to identify the number of bytes occupied till
it.
It will be Useful to identify the type of padding when we move into
new architectures.

Karthik Balaguru
 
B

Ben Pfaff

typedef struct{
char *abc;
dummy_struct *tmp;
char *xyz;
}my_struct;

I have pointer to tmp variable in this structure. Can I use offsetof()
function to access value stored in abc. Basically I want to get
pointer to my_struct. Is it possible using offsetof() function ?

Sure. This is done routinely, in fact. Here is one way:

my_struct *dummy_struct_to_my_struct (dummy_struct **dummyp)
{
/* Get 'dummyp' as a character point so we can adjust it
bytewise. */
char *p = (char *) dummyp;

/* Make 'p' point to the beginning of the my_struct. */
p -= offsetof (my_struct, tmp);

/* Return it converted to the proper pointer type. */
return (my_struct *) p;
}

This can in fact be written as one long expression.
 
J

Joe Wright

Hi,
I have following structure.

typedef struct{
char *abc;
dummy_struct *tmp;
char *xyz;
}my_struct;

I have pointer to tmp variable in this structure. Can I use offsetof()
function to access value stored in abc. Basically I want to get
pointer to my_struct. Is it possible using offsetof() function ?

Thanks,
No. my_struct is a struct type. No structure exists. Pointer to
my_struct is meaningless. Pointer to tmp variable in this structure is
meaningless. There is no value stored in abc because there is no structure.

offsetof() is not a function, but a macro. Maybe like..

#define offsetof(s_type, mbr) ((size_t) &((s_type *)0)->mbr)

...which takes a structure type and member name and evaluates the offset
of the member within the structure type.

Basically, I don't understand your question and I don't think you do either.
 
K

Keith Thompson

I have following structure.

typedef struct{
char *abc;
dummy_struct *tmp;
char *xyz;
}my_struct;

I have pointer to tmp variable in this structure. Can I use offsetof()
function to access value stored in abc. Basically I want to get
pointer to my_struct. Is it possible using offsetof() function ?

Basically, no.

I'm going to make some assumptions about what you mean.

You have an object of type my_struct, say:

my_struct obj;

You have a pointer to obj.tmp, say:

dummy_struct **ptr = &obj.tmp;

Given just the value of ptr, there's no way to determine that the
dummy_struct* object it points to is a member of a structure. If
my_struct has two members of type dummy_struct*, there'd be no way to
determine which one it points to.

To get the information you're looking for, you'd need to know which
my_struct object your pointer is pointing into -- but then you'd
already have a pointer to a my_struct.

If you want to know which my_struct object your pointer points into,
you can remember it when you obtain the pointer in the first place:

dummy_struct **ptr = &obj.tmp;
my_struct *ptr_points_into = &obj;

And in that case, you don't really need the pointer to obj.tmp; you
can just refer to ptr_points_into->tmp.

What problem are you really trying to solve? I suspect you started
with some specific problem and came up with the beginnings of a
solution that turned out to be unworkable. Now you're asking us how
to make your unworkable solution work. If you step back a bit and
think about your original problem, it's likely that we can help you
solve it (or that you can solve it yourself)
 
C

CBFalconer

I have following structure.

typedef struct{
char *abc;
dummy_struct *tmp;
char *xyz;
}my_struct;

I have pointer to tmp variable in this structure. Can I use
offsetof() function to access value stored in abc. Basically I
want to get pointer to my_struct. Is it possible using offsetof()
function ?

You don't have a stored value in abc. You only have a typedef.
After defining an object with:

my_struct obj;

you will have such a thing, and you can access "obj.abc" (a pointer
to a char), dereference, etc.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top