C 使用常量声明二维数组
我有一个游戏的头文件,它声明了棋盘的二维数组。
#ifndef GAME_H_
#define GAME_H_
static const int columns = 15;
static const int rows = 15;
int board[rows][columns];
#endif /* GAME_H_ */
我收到错误:
错误:在文件范围内对“board”进行了可变修改
I have a header file for a game that declares a two-dimensional array for a board.
#ifndef GAME_H_
#define GAME_H_
static const int columns = 15;
static const int rows = 15;
int board[rows][columns];
#endif /* GAME_H_ */
I get an error:
error: variably modified 'board' at file scope
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
C 不允许 const 变量作为数组边界。使用枚举代替:
C doesn't allow
const
variables as array bounds. Use an enum instead:该表达式是不允许的,它可以在 C++ 中使用,解决这个问题的唯一方法是像这样定义它:
That expression is not allowed, it can be used in C++, the only way around this is to define it like this:
从 C99 开始,您可以声明所谓的可变长度数组 (VLA),其中数组维度的大小是一个非常量表达式; IOW,您可以这样做:
请注意,这仅适用于 C99; C89 及更早版本要求您对数组维度使用编译时常量表达式。
VLA 的问题在于,由于它们的工作方式,它们只能在块范围内声明(即在函数内或函数中的复合语句内);它们可能不会被声明为
static
或extern
,并且它们可能不会在文件范围内声明(这是特定错误消息的来源)。在这种特殊情况下,您将需要使用编译时常量表达式(
const
限定的变量不是):请注意添加
extern 关键字。您不希望头文件中的声明成为数组的定义声明;相反,将定义声明放入实际实现游戏板的源文件中。否则,包含该标头的每个源文件将尝试为
board
创建自己的定义,并且将由链接器将其全部整理出来。As of C99, you can declare what is known as a variable length array (VLA) where the size of the array dimension is a non-constant expression; IOW, you can do this:
Note that this is only true for C99; C89 and earlier requires you to use compile-time constant expressions for the array dimension.
The problem with VLAs is that, because of how they work, they may only be declared at block scope (that is, within a function or a compound statement in the function); they may not be declared as
static
orextern
, and they may not be declared at file scope (which is the source of your particular error message).In this particular case, you will need to use compile-time constant expressions (which
const
-qualified variables are not):Note the addition of the
extern
keyword in the array declaration. You don't want the declaration in the header file to be the defining declaration for the array; instead, put the defining declaration in the source file that actually implements the game board. Otherwise every source file that includes that header will try to create its own definition forboard
and it will be up to the linker to sort it all out.无效 C。只能用常量定义数组,而不能用变量引用定义数组。即使变量引用引用的是常量值,行和列也是对常量值的引用,而不是常量值本身。
有多种方法可以使其按照您的意愿工作:
enum
的一部分,因为 C 编译器的枚举规则是在检测到类型不匹配时自动将它们转换为常量值。is not valid C. You can only define an array with a constant, not with a variable reference. Even if the variable reference refers to a value that is constant, rows and columns are references to the constant values an not the constant values themselves.
There are a number of ways to get this to work as you want:
enum
as the C compiler's rules for enumerations is to automatically covert them to their constant values whenever a type mismatch is detected.数组声明在编译前需要一个常量值。您可以在其中#define 变量或使用指针使其像数组一样工作。两种方法都可以给你相同的结果和简单性。
Array declarations need a constant value before compilation. You can #define variables in it or use pointers to make it work like an array. Both ways are fine will give you same results and simplicity.