'Possibly Incorrect Assignment' warnings from Turbo-C

Robert Klinkhammer klink at sce.carleton.ca
Wed Nov 29 16:05:19 AEST 1989


In article <3881.25715D5A at urchin.fidonet.org>, Bob.Stout at 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 
<klink at sce.carleton.ca>     for noncomformists anywhere" -- Asia
**********************************************************************



More information about the Comp.lang.c mailing list