String copy idiom.

Ron Natalie <ron> ron at brl-tgr.ARPA
Fri Mar 22 08:45:54 AEST 1985


> Beware of saying things like "the fastest possible".
> I found a better way for for any string longer than two characters.
> The following is from 4.2BSD on a VAX.
> 
> main()
> {
> 	register char *s, *d;
> 	while( *d++ = *s++ );			/* ok */
> 	if (*d) do {} while ( *d++ = *s++ );	/* <-- FAST -- */
> }
> 
Beware of posting solutions that don't work.  If *d == 0 you don't
copy anything at all.  I'm not quite sure why you put the test in,
all it does is make the loop dependent on whats in the destination
string before you copy into it.

-Ron



More information about the Comp.lang.c mailing list