tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: style: structures



>>> [...], 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?
> K & R, "The C Programming Language", second edition (english
> version), p 213:

> 	                                                      If a
> 	pointer to a structure is cast to the type of a pointer to its
> 	first member, the result refers to the first member.

!  Curious.  I would expect the standards to preserve that promise,
yes, but I haven't yet found anything equivalent in C99.  I've been
searching for things like "first (element|member)" with no success.

But this is not quite what you said (though admittedly "address" is
fuzzy enough it's not clear); the cast may involve significant changes
(for example, the resulting pointer may be larger than the original; I
*think* it might even be smaller, though I'm not sure - very little is
promised about pointer sizes).

Also, is there anything promising that casting back, that casting a
pointer to the type of the first element, which actually points to such
a first element, to a pointer to the containing struct type, returns a
pointer which points to that containing struct?

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse%rodents-montreal.org@localhost
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index