Mixing enumeration in switch-case

991 views Asked by At

What does C standard talk about mixing enumerations in the case constants of a switch-case statement? I ensure that there are no duplicate values of the mixed enum types.

switch (value) /* value is int type */
{
case enum1_val: /* enum1_val is of enum type enum1 */
    break;
case enum2_val: /* enum2_val is of enum type enum2 */
    break;
}

I get the code compiled clean with the -ansi -Wall flags, but Klockwork reports some problem in this code.

1

There are 1 answers

0
Yu Hao On BEST ANSWER

It's allowed in C. What you get is a warning by Klocwork because it thinks mixing different enum types is not a good idea. (I personally agree on this):

Quoting from Klocwork:

Inconsistent case labels

The INCONSISTENT.LABEL checker finds situations in which more than one enum type is used as a switch expression or as a label in a switch statement.

Vulnerability and risk

Using labels with different enum types in a switch statement can cause problems because enum members with the same value can have different meanings. The design intent fails, and unexpected results can occur.