Uses of offsetof?

J

Jack Klein

But I believe it's true that a pointer to a struct can be freely
converted to and from a pointer to the first element of that struct?

It can be converted, with a suitable cast. There are no automatic
conversions between pointers to objects of different types with the
exception of pointer to void. A pointer to any object type may be
converted to pointer to void without a cast, and vice-versa.

Since a structure cannot contain an instance of itself, the first
member of that struct is some other type of object. Therefore a cast
is always required to convert between a pointer to a struct and a
pointer to its first member.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
O

Old Wolf

Just out of personal curiosity :)

What do people use offsetof() for?

I use it in the table definition for a table-driven user
interface to edit members of a struct.
 
S

Stephen Sprunk

But I believe it's true that a pointer to a struct can be freely
converted to and from a pointer to the first element of that struct?

It can be freely converted _with a cast_, but without a cast (aka an
implicit conversion) it should have generated a compiler warning. If you
didn't get a warning, you need to turn up the diagnostic level on your
compiler (e.g. "-ansi -pedantic -W -Wall" for GCC).

Also, that conversion is only guaranteed to be correct in one direction.
Converting from an int* to a pointer-to-struct whose first element is an int
is legal, but trying to access any other members of that struct invokes UB
unless your int* originated from a pointer to that struct type -- and if it
did, why didn't you just keep the pointer-to-struct around and forget the
silly casts?

S
 
I

Iwo Mergler

Just out of personal curiosity :)

What do people use offsetof() for? I mean, I can understand why you'd
want to be able to take the address of a member of a struct, but you
can do that with just &(s.a) or similar. Why you'd care about the
offset (which surely depends on how the compiler chooses to lay the
struct out in memory), I don't really know. And if you did really
care, won't offset(s,a) just be &(s.a) - &s ?

It's used extensively inside the Linux kernel to implement
object inheritance in C.

You use offsetof to get at the parent of an object - aka
the structure which includes your current structure.

Kind regards,

Iwo
 
F

Francine.Neary

If you want a structure "t" that contains a sub-structure "s",
just include an actual "s" inside the "t":

struct S { ... whatever goes here ... };
struct T {
/* stuff before the S */
struct S s;
/* stuff after the S */
};

Then the sub-structure is just, e.g.:

struct T tmp;
...
operate(&tmp.s);

It is, of course, true that it is a bit less convenient to
write "tmp.s.field" everywhere you "want" to write "tmp.field".

Yes, that is quite ugly :(

I suppose another alternative is to add an extra level of indirection
to all the elements of the struct, then create a little routine,
structStosubstructT, say, that creates a new T struct and populates it
with the values of the pointers to what used to be just the elements
of S. But I can imagine this would be a pain to maintain, and you'd
need a separate routine for each struct/substruct pair.
 

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,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top