Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!bloom-beacon!bu-cs!dartvax!eleazar.dartmouth.edu!ari From: ari@eleazar.dartmouth.edu (Ari Halberstadt) Newsgroups: comp.lang.c Subject: Re: use of if (!cptr) and if (cptr), where cptr is a * Message-ID: <14580@dartvax.Dartmouth.EDU> Date: 21 Jul 89 16:27:35 GMT References: <10099@mpx2.mpx.com> <93@microsoft.UUCP> <10100@mpx2.mpx.com> <10103@mpx2.mpx.com> Sender: news@dartvax.Dartmouth.EDU Reply-To: ari@eleazar.dartmouth.edu (Ari Halberstadt) Organization: Dartmouth College, Hanover, NH Lines: 44 In article <10103@mpx2.mpx.com> erik@mpx2.mpx.com (Erik Murrey) writes: >Perhaps my example was bad. The above coding method is >even more useful in while() and for() loops: > >while (cc= fgetc(fp), cc != '\n') { > /* process more of line */ > ... >} That example is *certainly* not more useful. It is far better to use: while ((cc=fgetc(fp)) != '\n') It's standard (K&R used it all the time), and it's a little shorter. >or even: > >while (myptr= my_func(), myptr->x != myptr->y) { > /* ... */ > ... >} > >I would kinda like to see that one in your style... This example is much more appropriate, since I think it's the the only way todo this within the 'expr' part of the while loop. The other way to do it is: while (myptr = my_func() ) { if (myptr-> != myptr->y) break; /* ... */ } I think this latter method is used much more often. One place I've found the comma operator to be very useful is when an error must be handled and an error code returned from a function: if (error) return(perror("it failed"), FALSE); This saves the nuisance of using braces and two statements. while ((myptr-my_func())->x != -- Ari Halberstadt '91, "Long live succinct signatures" E-mail: ari@eleazar.dartmouth.edu Telephone: (603)640-5687 Mailing address: HB1128, Dartmouth College, Hanover NH 03755