[comp.lang.c] <type> x;

chris@mimsy.UUCP (Chris Torek) (01/20/88)

In article <2985@killer.UUCP> jfh@killer.UUCP (John Haugh) writes:
>here's the reference ...
>
>	C Language Reference Manual, D. M. Ritchie, 1978

(all kneel :-) )

>	Section 14.4, paragraph 4
>
>	"A pointer to one type may be converted to a pointer another
>	type.  The resulting pointer may cause addressing exceptions
>	if the subject pointer does not refer to an object suitably
>	aligned in storage.  It is guaranteed that a pointer to an
>	object of a given size may be converted to a pointer to an
>	object of a smaller size and back again without change."
		    ------- ----

>Sounds like the closest statement to being portable I can come up
>with.

Presumably `smaller size' means `sizeof(*p)' for that pointer results
in a smaller integer.  The one glitch with this definition is that
there is nothing that says that an object whose size is the *same*
as sizeof(char) has pointers that can be converted back and forth:

	short *sp;	/* some compiler with 8-bit shorts */
	char *cp;
	...
	printf("%d %d\n", sizeof(*sp), sizeof(*cp)); /* prints `1 1' */
	cp = (char *)sp;
	if (sp == (short *)cp) {
		/* always get here?  maybe! */
	}

I *will* say that it would have to be one cretinous compiler that
made sp != (short *)cp.

This sort of loose end (or loophole, if you will) is a large part
of what makes K&R not `a C standard'.  Language standards are an
awful lot like laws. . . .
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris