Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!cbatt!cbuxc!cbuxb!cbrma!aka From: aka@cbrma.UUCP (Andy Kashyap) Newsgroups: net.lang.c,net.bugs Subject: Re: C Compiler bug (and fix for a different one) Message-ID: <4835@cbrma.UUCP> Date: Thu, 31-Jul-86 14:09:57 EDT Article-I.D.: cbrma.4835 Posted: Thu Jul 31 14:09:57 1986 Date-Received: Sat, 2-Aug-86 04:44:36 EDT References: <273@watmath.UUCP> <5858@alice.uUCp> <134@sas.UUCP> Reply-To: aka@cbrma.UUCP (Andy Kashyap(Andy) x5292) Organization: AT&T-BL, RMAS, Columbus Lines: 58 Xref: watmath net.lang.c:10121 net.bugs:855 In article <134@sas.UUCP> jcz@sas.UUCP (Carl Zeigler) writes: > In article <5858@alice.uUCp>, ark@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: +---------------------------------------------------------------------------+