Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!harpsichord.cis.ohio-state.edu!lwh From: lwh@harpsichord.cis.ohio-state.edu (Loyde W Hales) Newsgroups: comp.lang.c Subject: Re: 'Possibly Incorrect Assignment' warnings from Turbo-C Message-ID: <74395@tut.cis.ohio-state.edu> Date: 27 Nov 89 16:56:57 GMT References: <256D8362.18B@marob.masa.com> Sender: news@tut.cis.ohio-state.edu Reply-To: Loyde W Hales Organization: Ohio State University Computer and Information Science Lines: 45 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(); > >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 ? Should >I change all such assignments to include an additional set of parens? > >if ((p = func())) > do_something() As far as I can tell, both statements are correct. (Now, I've been known to miss some pretty gross things, like my recent adventure in proving that you can't use *a[i] to mean both *a[i] and (*a)[i] -- less than ten lines from each other! :-) Turbo C will _always_ yield the `Possibly incorrect assignement' message if an assignment is the primary operation for a condition. This is not because it isn't valid; it is to flag two of the more common mistakes: if (a = b) instead of if (a == b) if (a = b != error) instead of if ((a = b) != error) You can 1. Turn off this error message for the code 2. Use the extra parens. 3. Ignore it. I'd suggest the last..only because I never turn off messages and find the first code example easier to read. LL -=- Department of Computer and Information Science Loyde W. Hales, II The Ohio State University lwh@cis.ohio-state.edu 2036 Neil Avenue Mall, Columbus, Ohio 43201