Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!nrl-cmf!cmcl2!adm!smoke!gwyn From: gwyn@smoke.ARPA (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: Explanation, please! Message-ID: <8375@smoke.ARPA> Date: 25 Aug 88 16:16:29 GMT References: <638@paris.ICS.UCI.EDU> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 38 In article <638@paris.ICS.UCI.EDU> schmidt@bonnie.ics.uci.edu (Douglas C. Schmidt) writes: - 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); - } -Now, much to my surprise, this is not only valid C++, it is also valid C! -Could some one please explain to me why this is so? Could you explain why it shouldn't be? "case" labels are just a special form of label. You can stick a label on most statements. -It seems like the case 7-1 labels are actually nested inside the -do {} while loop, and thus not in the scope of the switch ?? "Thus"? Why would the labels go out of scope? They're definitely within the body of the switch. -(should a break statement exit both the switch and the loop, or just one?!?!) A "break" applies to whatever the innermost container is. A "break" between two of the "*to++ = *from++;" statements would exit the do-while loop. -Finally, Stroustrup asks the rhetorical question ``why would anyone -want to write something like this.'' Any guesses?! It's about the fastest way to move arbitrarily-aligned data in portable C with a guarantee as to what happens in the case that the data overlaps. memcpy() doesn't guarantee anything for overlaps. memmove() does, but that's a recent X3J11 invention that probably doesn't exist on your system yet.