So at our university, they've been printing out the following piece of code on the coffee cups :
void f(char *x, char*y, int z)
{
int z2 = (z + 7) / 8;
switch (z % 8)
{
case 0:
do
{
*x++ = *y++;
case 7:
*x++ = *y++;
case 6:
*x++ = *y++;
case 5:
*x++ = *y++;
case 4:
*x++ = *y++;
case 3:
*x++ = *y++;
case 2:
*x++ = *y++;
case 1:
*x++ = *y++;
} while (--z2 > 0);
}
}
Running it, I've figured out it just copies z
bytes from the address pointed to by y
, into the address pointed to by x
.
It seems to me as if the code is just needlessly complicated and ugly , with the switch
statement and especially the confusing do while
loop beginning in case 0
and ending in case 1
.
Wouldn't a simple for loop suffice ? :
void ff(char *x, char *y, int z)
{
for (int i = 0; i < z; i++)
*x++ = *y++;
}
As far as I could tell, it does the same thing, unless there's something I'm missing?