conditional expression evaluation question

Gregory Smith greg at utcsri.UUCP
Tue Jan 20 04:52:55 AEST 1987


In article <207 at rebel.UUCP> george at rebel.UUCP (George M. Sipe) writes:
>I need to check a string, composed of byte triples, for a null area no
>less than MINSKIP triples in length.  A pointer, cp, is initialized to
>a triplet boundary.  After the test, it must remain on a triplet
>boundary.  Initially, I wrote the following:
>
>	while (cp < end && triples < MINSKIP)
>		if ((*cp++ | *cp++ | *cp++) == 0) ++triples;
>		else triples = 0;
>
> ... [problems with above] ...

Many have pointed out that the test clause should be something like
	cp[0] == 0 && cp[1] == 0 && cp[2] == 0
and the increment should be done separately with cp += 3 or cp = &cp[3].

Here's a solution using a pointer to an array. ( Watch carefully. At
no time do my fingers leave the keyboard...):

typedef char triple[3];
triple *tp,*end;	/* or char (*tp)[3], (*end)[3]; */
...
	while( tp < end && triples < MINSKIP ){
		if( (*tp)[0] == 0 && (*tp)[1] == 0 && (*tp)[2] == 0 )
			++triples;
		else triples = 0;
		++tp;		/* next triple */
	}

Note that (*tp)[1] should give the same code as cp[1], and ++tp should
be the same as cp += 3, so the difference is merely semantic, and the
question is whether you feel that the above makes more sense than
the cp solution. If a lot of things are done with these triples,
it can end up being somewhat cleaner, since a lot of '3' constants
become implicit in the addressing.

-- 
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg
Have vAX, will hack...



More information about the Comp.lang.c mailing list