void *

Ken Arnold%CGL arnold at ucsfcgl.UUCP
Fri Apr 25 05:36:56 AEST 1986


In article <728 at bentley.UUCP> kwh at bentley.UUCP (KW Heuer) writes:
>In article <1883 at ucsfcgl.UUCP> ucsfcgl!arnold (Ken Arnold%CGL) writes:
>>Purists will note that I really should say
>>	ptr = (some_type *) malloc(10 * sizeof *ptr);
>>But eventually malloc() will be of type "void *".
>
>Okay, enlighten me.  When I first saw the idea of "void *" I thought it was
>to be a name for the most restrictive pointer type ("ALIGN *" for those who
>know the malloc source).  Then when I rechecked, I found that it was a new
>name for the least restrictive pointer type, "char *".  Now you seem to say
>that the cast operator is unnecessary in your example.  I'd like some more
>detail about this beast.  Specifically, suppose we have the following:
>	void  *v;
>	char  *c;
>	short *h;
>	ALIGN *a;
>Of the 12 nontrivial assignment statements, which ones will ANSI C accept
>without a cast?  What will lint accept (with/without cast)?

Well, as *I* understand it (and my draft is, of course, out of date),
(void *) can be cast to any other pointer type without complaint.
So

	ptr = malloc( ... );

would be as legal as

	int_var = float_var;

i.e., the conversion would be automatic and silent (because it is
guaranteed to work properly).  Presumably, any other pointer assignment
would generate a warning unless accompained by a cast (and I sure hope
it is silent if I explicitly cast it).

		Ken Arnold



More information about the Comp.lang.c mailing list