C Compiler bug (and fix for a different one)

Andy Kashyap aka at cbrma.UUCP
Fri Aug 1 04:09:57 AEST 1986


In article <134 at sas.UUCP> jcz at sas.UUCP (Carl Zeigler) writes:
>  In article <5858 at alice.uUCp>, ark at alice.UucP (Andrew Koenig) writes:
>  > > So, does anyone have a fix for this bug?
>  > >
>  > > void f3(which)
>  > > {
>  > >     extern void f1(),f2();
>  > >     which?f1():f2();
>  > > }
>  > > cc(1) gives an "incompatible types" error.
>  > 
>  > As it should.  The only thing you're allowed to do with void values
>  > is throw them away.
>
>Scan again, Andrew, the (void) values are being thrown away.
>

No they are NOT; the value from the '?:' operator is being thrown away.
The '?:' operator expects a non-void value so it can decide 'which' and
pass the result to a higher level of expression (in this case there happens
to be none). The complaint of "incompatible types" is the result of the
'?:' operator expecting a non-void type as arguments and the arguments
providing a void type.
Wasn't that obvious from the diagnostics??? ;-)

To re-phrase, the '?:' doesn't look at its operands (parameters or arguments)
as function calls -- they might as well be 'f1()+5' or so -- but as
expressions. Therefore it '*RETURNS THE VALUE* of one of its operands
depending on the third'.

A semantic point of view:

A function call is an expression and can, therefore, be used anywhere an
expression can be used. When you declare a function (void), you state that
you intend to use that function as a statement instead, that you do not 
intend to use it in any operations. It can now only be used where a statement
can. Keep in mind that an expression is a statement, but NOT vice-versa.

If you look up the reference section of K&R, somewhere it says something
like this (I don't have my K&R with me):
	...
	expression -> expression ? expression : expression
	...
Thus you can not use a statement (ie (void) f1()) where an expression is
expected.

The BUG: there ain't none.
The FIX: use 'if' instead.

- andy kashyap
-- 

+---------------------------------------------------------------------------+
: Jim, what's the value of Pi?                         : Andy Kashyap       :
: About 3.14159. Why do you ask, Doctor?               : AT&T Bell Labs     :
: Actually, Captain, the exact value is 3.1415926535...: Columbus OH        :
: Kirk & McCoy: Shut Up, Spock!!!                      : ..!cbosgd!cbrma!aka:
+---------------------------------------------------------------------------+



More information about the Net.bugs mailing list