Do you have to cast void pointers when dereferencing them?

Chris Torek chris at mimsy.UUCP
Sun Dec 18 02:26:11 AEST 1988


In article <3050 at arcturus> evil at arcturus.UUCP (Wade Guthrie) writes:
>but this seems to indicate that the dpANS performs implicit type
>conversions for pointers.  

dpANS C does, but only for

	pointer to T => pointer to void

and

	pointer to void => pointer to T

where T is any type other than `pointer to void' (for which conversion
is unnecesssary) and perhaps `pointer to function returning T1'.  (It
is not entirely clear to me that the hack we used to allow

	char *pc; void *pv; pc = pv; pv = pc;

in 4BSD PCC is wrong, but it does also allow

	void *pv; int (*pfi)(); pv = pfi; pfi = pv;

and I am suspicious that this *is* wrong.)

>To say that pointer_of_one_type = pointer_of_another_type is legal, the
>code must know the type of the lvalue to put the rvalue into the proper
>representation.  Is this not true?

It is true.  But the compiler does indeed know both types.  In the
dpANS, if at least one of those types is `pointer to void' (and the
other provisos above hold true) the assignment is legal; otherwise,
such an assigment is legal only if the assignment is the result of a
cast: (pointer_of_one_type) pointer_of_another_type, e.g.,

	int i;
	(short *)&i

(remember that a cast is equivalent to assignment to an unnamed
temporary variable---I like to consider `assignment' to include all
casts).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list