"do ... while ((NULL + 1) - 1);" -- valid C?

david.f.prosser dfp at cbnewsl.ATT.COM
Sat Aug 12 03:59:24 AEST 1989


In article <18996 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>
>	ip = (int *)((char *)0);
>
>On *most* machines, this `just happens' to work.  But if we look very
>closely at the language definition, we find that it is not *required*
>to work.  The version of this that is required to work is instead
>
>	ip = (int *)(char *)(int *)0;
>
>We are not allowed (outside of machine-dependent code) to change a
>pointer-to-char into a pointer-to-int unless the pointer-to-char itself
>came into existence as the result of a cast from a pointer-to-int.  The
>only way to *create* a nil-pointer-to-int in the first place is to
>write (int *)0, or (in the proposed ANSI C) (int *)(void *)0.

The pANS does guarantee that, for example,

	0 == (void *)(int *)(char *)0

[3.2.2.3: "Two null pointers, converted through possibly different sequences
of casts to pointer types, shall compare equal."]

Therefore, I interpret the pANS as requiring

	(int *)(char *)0

to have the same value as

	(int *)0

(the nil-pointer-to-int, in your terminology)--not `just happening' to work.

Dave Prosser	...not an official X3J11 answer...



More information about the Comp.lang.c mailing list