'Possibly Incorrect Assignment' warnings from Turbo-C

Loyde W Hales lwh at harpsichord.cis.ohio-state.edu
Tue Nov 28 03:56:57 AEST 1989


In article <256D8362.18B at marob.masa.com> daveh at 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 at cis.ohio-state.edu          2036 Neil Avenue Mall, Columbus, Ohio  43201



More information about the Comp.lang.c mailing list