use of if (!cptr) and if (cptr), where cptr is a *
Ari Halberstadt
ari at eleazar.dartmouth.edu
Sat Jul 22 02:27:35 AEST 1989
In article <10103 at mpx2.mpx.com> erik at 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 at eleazar.dartmouth.edu Telephone: (603)640-5687
Mailing address: HB1128, Dartmouth College, Hanover NH 03755
More information about the Comp.lang.c
mailing list