开关案例:错误:case标签不会减少整型常量 [英] switch case: error: case label does not reduce to an integer constant

查看:955
本文介绍了开关案例:错误:case标签不会减少整型常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int value;

const int signalmin = some_function();

switch(value)
{
   case signalmin:
   break;
}

我看了some_function的值,并使用该int值做上的开关情况。 C99编译器还给:

I read the value of some_function and use that int value to do a switch case on. The C99 compiler gives back:

错误:case标签不会减少整型常量

error: case label does not reduce to an integer constant

但交换机执行之前,我不能使用的#define,因为int值被读取。

But I cannot use a #define because the int value is being read before the switch executes.

推荐答案

开关标签必须不断前pressions,他们必须在编译时进行评估。如果你想在运行时的值分支,则必须使用如果

switch labels must be constant expressions, they have to be evaluated at compile time. If you want to branch on run-time values, you must use an if.

A 常量 -qualified变量不是的恒前pression 的,它只不过是你不能修改的值。

A const-qualified variable is not a constant expression, it is merely a value you are not allowed to modify.

整型常量前pressions的形式详细介绍了6.6(6)C99和C2011标准n1570草案]:

The form of integer constant expressions is detailed in 6.6 (6) [C99 and the n1570 draft of the C2011 standard]:

6的的整型常量前pression 的应有整数类型,并应只有操作数
  是整型常量,枚举常量,字符常量,的sizeof
  前pressions,其结果是整型常量, _Alignof 前pressions和浮动
  是强制转换的立即数常量。在一个整型常量类型转换操作符
  前pression只能转换算术类型为整数类型,除了一个组成部分
  操作数的的sizeof _Alignof 运营商。

6 An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, _Alignof expressions, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof or _Alignof operator.

的限制,只有的sizeof 前pressions,其结果是一个整型常量被获准离开规则的sizeof 前pressions,其操作数是一个可变长度的数组。

The restriction that only sizeof expressions whose result is an integer constant are allowed rules out sizeof expressions whose operand is a variable length array.

这篇关于开关案例:错误:case标签不会减少整型常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆