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)