offsets in structures.

Guy Harris guy at rlgvax.UUCP
Sun Oct 14 10:19:37 AEST 1984


> >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
> >
> 
> As John Bruner has already pointed out, this is not at all broken behavior on
> the part of the compiler.  Section 7.14 of the C Reference Manual (p. 192 in
> K&R) says, " . . . it is guaranteed that assignment of the constant 0 to a
> pointer will produce a null pointer distinguishable from a pointer to any
> object."  This means that although "(struct foo *)0" is indeed of type
> "pointer to 'struct foo'", it is guaranteed not to point to any object, so it
> is meaningless to refer to the "next" object.

Yes, but would you trust any compiler that optimized

	(int) ((struct foo *)0 + 1)

into 1 not to optimize

	(int) ((struct rp11_regs *)177550 + 1)

into 177551?  At best, it may just be optimizing (0 of any kind) + N into N;
it should still check the kind of 0 before it forges ahead with this
optimization.

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



More information about the Comp.lang.c mailing list