statement(switch语句)可根据一个整数索引值(integer index)进行多重分支,处理有多种可能结果的测试时,这种语句特别有用。当开关情况数量比较多(如4个以上),并且值得范围跨度比较小时,GCC编译switch语句时会使用跳转表(jump table)这种数据结构;当不满足以上情况时编译方式和编译多个if-else语句的方式一样。这两种方式相比,跳转表的优点是执行开关语句的时间与开关情况的数量无关。
下面是引用GCC文档中的一段文字,具体解释了jump table的用法:
You can get the address of a label defined in the current function (or a containing function) with the unary(一元的) operator ‘&&’. The value has type void *. This value is a constant(常量) and can be used wherever a constant of that type is valid. For example:
1. void *ptr;
2. /* ... */
3. ptr = &&foo;
To use these values, you need to be able to jump to one. This is done with the computed goto statement1, goto *exp;. For example,
1. goto *ptr;
Any expression of type void * is allowed. One way of using these constants is in initializing a sta