> Date: Tue, 8 Oct 2024 08:50:00 -0400 (EDT) > From: Mouse <mo...@rodents-montreal.org> > > > [...], using the C standard guaranteed property that the address of > > the first member is the address of the structure, allowing to cast > > pointers in order to operate whether on the base structure or on the > > derived structure? > > Which standard promises this, and what exactly does it promise?
C99 Sec. 6.7.2.1 `Structure and union specifiers', clause 13, p. 103: `A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning.' Same text in C11 (clause 14, p. 115), and in C23 (clause 17, p. 104). But don't use casts for this -- use container_of, which is more flexible and checks types better than a cast does.