简体   繁体   English

C使用常量声明2D数组

[英]C declaring a 2d array using constants

I have a header file for a game that declares a 2d 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 ". 我收到一个错误“ error: variably modified 'board' at file scope

C doesn't allow const variables as array bounds. C不允许将const变量作为数组边界。 Use an enum instead: 改用枚举:

enum { columns = 15, rows = 15 };

That expression is not allowed, it can be used in C++, the only way around this is to define it like this: 该表达式是不允许的,它可以在C ++中使用,解决该问题的唯一方法是像这样定义它:

#ifndef GAME_H_
#define GAME_H_

#define COLUMNS 15
#define ROWS 15

int board[ROWS][COLUMNS];

#endif /* GAME_H_ */

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; 从C99开始,您可以声明所谓的可变长度数组 (VLA),其中数组维的大小是一个非常数表达式; IOW, you can do this: IOW,您可以执行以下操作:

int foo()
{
  int x = 5;
  int y = 10;
  int values[x][y];
  ...
}

Note that this is only true for C99; 注意,这适用于C99。 C89 and earlier requires you to use compile-time constant expressions for the array dimension. C89和更早版本要求您对数组维使用编译时常量表达式。

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); VLA的问题在于,由于它们的工作方式,它们只能在块范围内声明(即在函数内或函数中的复合语句内); they may not be declared as static or extern , and they may not be declared at file scope (which is the source of your particular error message). 它们可能未声明为staticextern ,也可能未在文件范围内声明(这是您特定错误消息的来源)。

In this particular case, you will need to use compile-time constant expressions (which const -qualified variables are not ): 在这种特殊情况下,您将需要使用编译时常量表达式( const限定变量不是 ):

#define COLUMNS 15
#define ROWS    15

extern int board[ROWS][COLUMNS];

Note the addition of the extern keyword in the array declaration. 请注意,在数组声明中添加了extern关键字。 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 for board and it will be up to the linker to sort it all out. 否则,每个包含该标头的源文件都将尝试为board创建其自己的定义,并且将由链接器将其全部整理出来。

int board[rows][columns];

is not valid C. You can only define an array with a constant, not with a variable reference. 是无效的C。您只能使用常量而不是变量引用来定义数组。 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: 有多种方法可以使它按需工作:

  1. You could define the "variables" in the preprocessor, so they get flattened into constants just before compile time. 您可以在预处理器中定义“变量”,以便在编译之前将它们展平为常量。
  2. You could define the values as part of an enumeration enum as the C compiler's rules for enumerations is to automatically covert them to their constant values whenever a type mismatch is detected. 您可以将值定义为枚举enum一部分,因为C编译器的枚举规则将在检测到类型不匹配时自动将其转换为常数值。

Try this 尝试这个

See if the above link helps you out any. 看看上面的链接是否对您有帮助。

array declarations need a constant value before compilation. 数组声明在编译之前需要一个常量值。 You can #define variables in it or use pointers to make it work like an array. 您可以在其中#define变量或使用指针使它像数组一样工作。 Both ways are fine will give you same results and simplicity. 两种方法都可以,给您相同的结果和简单性。

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

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