offsets in structures.

Guy Harris guy at rlgvax.UUCP
Fri Oct 12 08:15:54 AEST 1984


> Watch out!  I did a similar thing to calculate the width of a structure:
> 
> 	(int) ((struct foo *)0 + 1)
> 
> This would give me the sizeof a struct foo, ROUNDED UP to the necessary
> alignment.  It worked fine, until one day, I ported my program to an
> IBM PC with a Lattice C compiler.  There it would always evaluate to 1
> (apparently the compiler cancelled the two casts against each other).

That's a broken compiler.  "(struct foo *)0" is of type "pointer to 'struct
foo'", so adding 1 to it should make it point to the "next" object of type
"struct foo".

	Guy Harris
	{seismo,ihnp4,allegra}!rlgvax!guy



More information about the Comp.lang.c mailing list