void avoidance

Guy Harris guy at rlgvax.UUCP
Tue Oct 11 07:14:19 AEST 1983


oid) 1

but why should the compiler bother to detect a cast of a constant to a void
and allow other expressions?  One might as well be general, as there is no
penalty...

2)

	(void) ++i;

*CAN* be useful.  Assume there is a macro like "putc" which is intended to
look like a subroutine with side-effects and which also returns a value.
Say you want to invoke the macro for its side effects and ignore its value.
You can do that just by invoking it and not assigning the result to anything
but "lint" will yell at you.  The only way to shut it up is to cast its
result to void.  (This is not a hypothetical example; I have seen reasonable
code which has run across this problem.)

What harm does this facility do?  I can't speak for Algol 68, which does
have some sort of notion of void types, but it is very useful in C to throw
away the result of a procedure called only for its side-effects.

*I* suspect these rules were invented for just these purposes (there is
explicit code in PCC/lint which understands casts to voids, so the ability
to cast an expression to void did not "just 'come out in the wash'" there).

	Guy Harris
	{seismo,mcnc,we13,brl-bmd,allegra}!rlgvax!guy



More information about the Comp.lang.c mailing list