Context:
void strcpy (char *s, char *t)
{
while (*s++ = *t++);
}
"Kerry Thompson" <[EMAIL PROTECTED]> wrote:
> andrew clarke wrote:
> > Are you sure the boolean comparison isn't done first, before
> > the copy?
>
> Yup. Strings in C are terminated with ASCII 0.
No, strings are terminated with a null (zero valued) byte, ASCII
or otherwise.
> When that 0 is copied, *s == 0,
Yes.
> which is equivalent to false.
Well, *s is false, so *s == 0 would be true. The subsequent ++
complicates the issue since s has moved on in the expression
in question.
> The while loop terminates, and s and t are
> not incremented.
No, s and t are always incremented no matter what the condition
result.
> Moshe is technically right. This is an assignment, not a boolean
> comparison.
There is an implicit comparison.
> But a "while" loop acts on a true/false--or, more accurately,
> 0/!0--condition. So, pardon my shorthand, but it acts the same
> as a boolean comparison.
More precisely, any conditional expression is compared with zero...
while (expr)
...is equivalent to...
while ((expr) != 0)
> > The above is also one of the more difficult for programmers
> > new to C (and even old hacks like me!) to understand at first
> > glance.
True, but on a 68k machine the while loop is a metaphore for...
@1 move.b (a0)+, (a1)+
bne.s @1
Which struck me as particularly elegant.
> > If I saw this sort of thing in modern "production" code I
> > wouldn't be very happy.
Neither would I, I'd prefer the semicolon on a separate line.
> > There is no harm in expanding it out to aid readability and
> > debugging.
>
> I agree.
I don't. This loop is idiomatic. It's like saying you don't like
Aboriginal art because of all the ugly dots. But the dots _are_
the art. ;-)
--
Peter
To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>.
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/c-prog/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/