Path: utzoo!attcan!uunet!virtech!cpcahil From: cpcahil@virtech.uucp (Conor P. Cahill) Newsgroups: comp.lang.c Subject: Re: 'Possibly Incorrect Assignment' warnings from Turbo-C Message-ID: <1989Nov25.134246.16025@virtech.uucp> Date: 25 Nov 89 13:42:46 GMT References: <256D8362.18B@marob.masa.com> Organization: Virtual Technologies Inc. Lines: 49 In article <256D8362.18B@marob.masa.com>, daveh@marob.masa.com (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: > > char *p, *func(); > > if (p = func()) > do_something(); I think this kind of warning should be generated by all compilers. What you did is legal C, but when you go back to it after two or three weeks you too will have to look at the surrounding code to see if it was supposed to be an assignment or comparison operator. There are several other ways that the same code could be written that would be just as efficient and not be confusing. How about: p = func(); if( p )... Or if( (p=func()) != 0 )... In both of these cases you know that func is assigned to p and the result is compared to zero. > Am I fooling myself by ass/u/ming that func() will always be invoked > and its return value assigned to p, before p is evaluated ? No func will always be called and the result will be assigned to p. The compiler was only warning you that you might have made the same mistake that every C programmer has made at least once: using an assignment operator when a comparitive operator was intended (i.e. = instead of ==). > Should > I change all such assignments to include an additional set of parens? This may or may not quiet the compiler, but it does nothing to assure the reader of the intended operator. -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+