简体   繁体   English

为什么我不能使用常量 int 声明一个数组并在 C 中对其进行初始化?

[英]Why can't I use a constant int to declare an array AND initialize it in C?

Option 1: use #define选项 1:使用#define

#define kSize 5
int arr1[kSize] = {1,2,3,4,5};

--> OK. --> 好的。

Option 2: use an enum选项 2:使用enum

enum { eSize = 5 };
int arr2[eSize] = {1,2,3,4,5};

--> OK. --> 好的。

But, a const int cannot be used:但是,不能使用const int

const int cSize=5;
int arr3[cSize] = {1,2,3,4,5};

--> FAIL. --> 失败。

why?为什么?

A variable with the const qualifier does not qualify as an integer constant expression .具有const限定符的变量不符合integer 常量表达式的条件。 This makes the array a variable length array (VLA) which cannot be initialized.这使数组成为无法初始化的可变长度数组(VLA)。

Section 6.7.6.2p4 of the C standard describing Array Declarators states: C 标准的第 6.7.6.2p4 节描述了数组声明符状态:

If the size is not present, the array type is an incomplete type.如果大小不存在,则数组类型是不完整的类型。 If the size is * instead of being an expression, the array type is a variable length array type of unspecified size,which can only be used in declarations or type names with function prototype scope such arrays are nonetheless complete types.如果大小是*而不是表达式,则数组类型是未指定大小的变长数组类型,它只能用于声明或类型名称中,function 原型 scope 这样的 ZA3CBC3F9D0CE2F2C1554E1B671 类型仍然是完整的。 If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type;如果大小是 integer 常量表达式并且元素类型具有已知的常量大小,则数组类型不是变长数组类型; otherwise, the array type is a variable length array type.否则,数组类型是可变长度数组类型。 (Variable length arrays are a conditional feature that implementations need not support; see 6.10.8.3.) (可变长度 arrays 是实现不需要支持的条件特性;参见 6.10.8.3。)

So for an array to not be a variable length array its size must be an integer constant expression.因此,如果数组不是可变长度数组,它的大小必须是 integer 常量表达式。 This is defined in section 6.6p6:这在第 6.6p6 节中定义:

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. 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 常量表达式中的强制转换运算符只能将算术类型转换为 integer 类型,除非作为sizeof_Alignof .operator 的操作数的一部分

A #define definition is replaced by the preprocessor before the compilation phase, so in your first case kSize is exactly the same as the constant 5 . #define定义在编译阶段之前被预处理器替换,因此在第一种情况下kSize与常量5完全相同 The above passage also states than an enum constant qualifies as an integer constant expression, so this makes your second case OK.上面的段落还指出,枚举常量符合 integer 常量表达式,因此这使您的第二种情况正常。 The third case uses a const qualified variable which is not included above in the definition of an integer constant expression, so this makes it a variable length array.第三种情况使用const限定的变量,该变量包含在 integer 常量表达式的定义中,因此这使其成为可变长度数组。

Section 6.7.9p3 then dictates what can be initialized:然后第 6.7.9p3 节规定了可以初始化的内容:

The type of the entity to be initialized shall be an array of unknown size or a complete object type that is not a variable length array type .要初始化的实体的类型应该是一个未知大小的数组或一个完整的 object 类型而不是可变长度数组类型

And as stated above a VLA cannot be initialized.如上所述,无法初始化 VLA。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM