Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uunet!mitel!sce!klink From: klink@sce.carleton.ca (Robert Klinkhammer) Newsgroups: comp.lang.c Subject: Re: 'Possibly Incorrect Assignment' warnings from Turbo-C Summary: Kludge no longer required. Message-ID: <728@sce.carleton.ca> Date: 29 Nov 89 05:05:19 GMT References: <3881.25715D5A@urchin.fidonet.org> Organization: Systems & Computer Eng. Dept., Carleton University, Ottawa, Canada Lines: 38 In article <3881.25715D5A@urchin.fidonet.org>, Bob.Stout@p6.f506.n106.z1.fidonet.org (Bob Stout) writes: ] In an article of <24 Nov 89 18:07:29 GMT>, (Dave Hammond) writes: ] ] >In porting a program from Unix to Dos, I get an error from Turbo-C ] >`Possibly incorrect assignment' with code similar to the following: ] > ] >if (p = func()) ] > do_something(); ] ] The warning (not an error) will go away if you use instead: ] ] if (0 != (p = func())) ] do_something(); ] ] The warning exists because it saw an assignment operator where it expected a ] logical comparison operator. Since typing `=' when you mean `==' is a fairly ] common mistake, it always warns you with the "Possibly incorrect assignement" ] message. The warning will also go away if you do: if ((p = func()) != 0) do_something(); which (IMHO) expresses the intent more clearly, and is easier to read than the above. The original reason for reversing the order of the operands to a test for equality, was, I think, to protect against the exact type of error mentioned above. But since Turbo C emits a warning when you type `=' instead of `==', it would seem that the reversal buys nothing. It is also possible to turn off this warning altogether, by passing the flag "-w-pia" to the Turbo C compiler. This is documented somewhere in the depths of their extremely verbose manuals. -- ********************************************************************** Robert Klinkhammer "They recommended euthanasia for noncomformists anywhere" -- Asia **********************************************************************