Assignment in test: OK?

D'Arcy J.M. Cain darcy at druid.uucp
Sat Sep 15 00:19:25 AEST 1990


In article <18326 at ultima.socs.uts.edu.au> jeremy at sinope.socs.uts.edu.au (Jeremy Fitzhardinge) writes:
>this is quite a useful construct.  How many would prefer
>	if (thing != NULL)
>	{
>		fp = fopen(thing, "r");
>		if (fp == NULL)
>			barf();
>	}
>to
>	if (thing && (fp = fopen(thing, "r"))
>		barf();
>?
I think I prefer the former since the second version has a bug.  I think you
meant to say:
    if (thing && ((fp = fopen(thing, "r")) == NULL)
and just as a matter of style I would use:
    if ((thing != NULL) && ((fp = fopen(thing, "r")) == NULL)

And that solves the =/== problem as well.

I suppose you may also have meant
    if ((thing == NULL) || ((fp = fopen(thing, "r")) == NULL)
unless the 'file not named' error is handled elsewhere.

-- 
D'Arcy J.M. Cain (darcy at druid)     |
D'Arcy Cain Consulting             |   MS-DOS:  The Andrew Dice Clay
West Hill, Ontario, Canada         |   of operating systems.
+ 416 281 6094                     |



More information about the Comp.lang.c mailing list