【发布时间】:2011-11-28 22:23:29
【问题描述】:
可能重复:
C/C++: switch for non-integers
C/C++ switch case with string
我将字符串传递给函数 get_band(char *str) 然后我将传递的参数与特定的字符串进行比较
if(strcmp(str, "auto"))
{
//do something
}
我的问题是 - 因为我有很多要比较的字符串,我如何使用 switch 语句而不是 if/else,因为 switch 语句只支持整数类型。
【问题讨论】:
-
请注意,当 str 与 'auto' 不匹配时,您的语句将执行 'do something'
-
计算一些字符串的哈希值?
-
@KevinDTimm 很好地抓住了那里脱帽
-
还要注意 strcmp 返回 -1、0 和 1 - 当 str 在词法上小于、等于或大于“auto”时
标签: c switch-statement