本人在C语言中定义了一些const量,用作数组长度的时候,VC提示本人说“表达式必须含有常量值”
莫非const的不算常量吗?怎么的才算常量?
莫非const的不算常量吗?怎么的才算常量?
const int N = 15; int arr[N];
第二行,提示N那个地方错误,“表达式必须含有常量值”。
解决方案
10
C(C89) 中 const 仅仅表示一个变量是不可修改的,而不具备声明一个常量的功能。
15
在 C 语言里,不是。
C 语言的 const expression 是如下定义的:
6.6 Constant Expressions
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.
也就是说可以包括:
integer constants 在 C 语言里仅包括 1, 23, 123 之类的字面量。
enumeration constants ,枚举值
chracater constants, 字符常量,”a”, “1”,同样仅限字面量
sizeof(…)
等
在你的程序里,N 不属于以上任何一类。
C 语言的 const expression 是如下定义的:
6.6 Constant Expressions
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.
也就是说可以包括:
integer constants 在 C 语言里仅包括 1, 23, 123 之类的字面量。
enumeration constants ,枚举值
chracater constants, 字符常量,”a”, “1”,同样仅限字面量
sizeof(…)
等
在你的程序里,N 不属于以上任何一类。