'Possibly Incorrect Assignment' warnings from Turbo-C
Conor P. Cahill
cpcahil at virtech.uucp
Sun Nov 26 00:42:46 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();
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 |
+-----------------------------------------------------------------------+
More information about the Comp.lang.c
mailing list