From Zed Shaw's, Learn C the Hard Way,in exercise 23, he talks about Duff's Device. Here is Duff's Device for reference:
int duffs_device(char *from, char *to, int count)
{
{
int n = (count + 7) / 8;
switch(count % 8) {
case 0: do { *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while(--n > 0);
}
}
return count;
}
He asks the reader to:
"Create a set of macros that lets you create any length device like this. For example, what if you wanted to have 32 case statements and didn't want to write out all of them? Can you do a macro that lays down 8 at a time?"
This really stumped me and I feel I just need a nudge in the right direction. Any help would be greatly appreciated!
Something like this:
will expand into
Update:
Or, you can rewrite the first macro:
Which is basically the same, except that it doesn't use octal numbers.