Finding NULL byte in a long

Lars Wirzenius wirzeniu at cs.Helsinki.FI
Tue Dec 11 22:40:26 AEST 1990


In article <198 at nazgul.UUCP> bright at nazgul.UUCP (Walter Bright) writes:
>In article <1990Dec5.033206.10463 at nimbus3.uucp> djs at nimbus3.UUCP (Doug) writes:
>/I know this has been on the net before, since I thought I saved it,
>/but can't find it now.  Anyway, could someone tell me what the
>/C expression is that tells you if a long has a NULL byte in it.
>/This is without masking each byte and testing it for 0.  It is very
>/clever and non-obvious.  Thanks.
>
>Hmmm, how about:
>	p = memchr(&x,0,sizeof(x));

How about (assuming sizeof(x) == 4, easily modified for other sizes):

	((char *) &x)[0] == 0 ||
	((char *) &x)[1] == 0 ||
	((char *) &x)[2] == 0 ||
	((char *) &x)[3] == 0

Lars Wirzenius    wirzeniu at cs.helsinki.fi    wirzenius at cc.helsinki.fi



More information about the Comp.lang.c mailing list