- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
int value;
const int signalmin = some_function();
switch(value)
{
case signalmin:
break;
}
我读取了 some_function 的值并使用该 int 值来打开 switch case。 C99 编译器返回:
error: case label does not reduce to an integer constant
但我不能使用 #define
,因为在执行 switch 之前正在读取 int 值。
最佳答案
switch
标签必须是常量表达式,它们必须在编译时求值。如果您想在运行时值上进行分支,则必须使用 if
。
const
限定的变量不是常量表达式,它只是一个不允许修改的值。
整数常量表达式的形式详见6.6(6)【C99和C2011标准的n1570草案】:
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 thesizeof
or_Alignof
operator.
仅允许结果为整数常量的 sizeof
表达式的限制排除了操作数为可变长度数组的 sizeof
表达式。
关于c - 开关盒 : error: case label does not reduce to an integer constant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14069737/
所以如果我在 C++ 中有这样的东西: char A_char = 'A'; char * myPtr = &A_char; const char * myPtr = &char_A; //point
我试图在我的 Perl 脚本中将魔数(Magic Number)声明为常量,如 perlsub 中所述。但是,我收到警告: $ cat foo.perl use warnings ; use stri
我想为 data Constant a b = Constant a 这是我的直接尝试: instance Foldable (Constant a) where foldr f b (Const
我在客户端和服务器端拆分了我的文件夹,但我没有从父文件夹工作,我表现得好像它们是 2 个不同的文件夹...现在我想部署到 Heroku 但我为此需要一个主文件夹,所以我想更改我的 webpack.co
当函数不修改对象参数时,我总是让它请求一个常量引用,即使引用的对象不是真正的常量。这是错误的吗? 对于包装类,我想这样写: template class Wrapper{ private: B*
核心常量表达式的定义取决于常量表达式的概念,如要点 (2.7.1) 和 (2.9.1) 所示N4140 的。 §5.19/2: A conditional-expression e is a core
我有以下代码片段,它按预期工作。其中 x 是一个变量 var myVariable = (x === 'A' || x=== 'B') ? 'sui' : 'pai'; 但是闭包编译器正在将它转换为
我是一个国际化应用程序。其中一部分在于菜单的国际化。没关系。 通过 GWT,我可以使用 Constants 接口(interface)。 现在我必须国际化该应用程序的帮助,其中包括一些涉及菜单的文本。
在 Bjarne Stroustrup 的 A Tour of C++ 中,每章末尾都列出了一些建议。在第一章的结尾,其中一个写道: Avoid ‘‘magic constants;’’ use sy
创建常量数组的常量数组的语法是什么? 我希望函数参数是常量 char* 字符串的常量数组。 最佳答案 您可以通过将 const 放在第一个星号的右侧来实现,例如 void f(const char *
我有一本带图书馆的 Chef Recipe ,例如库.rb。它包含一个 CONSTANT: CONSTANT = 'constant' 当我为这本 Recipe 编写单元测试时,它总是给我警告: (S
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How to check for equals? (0 == i) or (i == 0) Why does
我有以下代码: constexpr unsigned long long power_function(const unsigned long long prime, const unsigned l
在一个页面上,我有几个 Angular 模块。我为每个模块定义了一个包含模块版本的常量。 var module1 = angular.module('module1').constant('versi
C++14 中的§5.19/3 定义了一个整型常量表达式和一个转换后的常量表达式: An integral constant expression is an expression of integr
如果您打开 R# 选项并转到代码编辑> C# > 命名样式,则有 2 个设置与我非常相似。本地常量和常量字段(私有(private))。一种是 lowerCaseCamel,另一种是 UpperCam
如何将恰好命名为 reverse 的以下方法重写为允许任何枚举类型的泛型方法。 public class TestX { enum Gender { male, female } pu
我和我的一位队友进行了一次有趣的谈话。 CONSTANT.equals(VARIABLE) 是否比 Java 中的 VARIABLE.equals(CONSTANT) 快? 我怀疑这是一个虚假陈述。但
我想在 c 程序中执行脚本 cmd,所以函数 SYSTEM(CONST CHAR) 可以执行它,但我想使用这个函数和 3 个不同的参数写入一次。谁能帮帮我,有没有那种功能。 最佳答案 如果我猜对了,您
VStudio 或 ReSharper 给我以下建议: constant 在这种情况下意味着什么?如果它是当前方法作用域中的一个常量,目的是什么?方法往往很小,因此与常规 var 相比,它不应该有任何
我是一名优秀的程序员,十分优秀!