In fact, the switch statement works the way you noticed it. It is designed in such a way that you can combine several cases together until a break occurs and it acts like a sieve.
Here is a real world example from one of my projects:
struct keystore_entry *new_keystore(p_rsd_t rsd, enum keystore_entry_type type, const void *value, size_t size) {
struct keystore_entry *e;
e = rsd_malloc(rsd, sizeof(struct keystore_entry));
if ( !e )
return NULL;
e->type = type;
switch (e->type) {
case KE_DOUBLE:
memcpy(&e->dblval, value, sizeof(double));
break;
case KE_INTEGER:
memcpy(&e->intval, value, sizeof(int));
break;
case KE_STRING:
if ( size == 0 ) {
size = strlen((const char *)value);
}
case KE_VOIDPTR:
e->ptr = rsd_malloc(rsd, size);
e->size = size;
memcpy(e->ptr, value, size);
break;
default:
return NULL;
}
return e;
}
Code for cases KE_STRINGand KE_VOIDPTRis identical except in the case of calculating the size of the line.
source
share