C 使用常量声明二维数组

发布于 2024-09-30 19:51:18 字数 269 浏览 3 评论 0原文

我有一个游戏的头文件,它声明了棋盘的二维数组。

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

可可 2024-10-07 19:51:18

C 不允许 const 变量作为数组边界。使用枚举代替:

enum { columns = 15, rows = 15 };

C doesn't allow const variables as array bounds. Use an enum instead:

enum { columns = 15, rows = 15 };
终陌 2024-10-07 19:51:18

该表达式是不允许的,它可以在 C++ 中使用,解决这个问题的唯一方法是像这样定义它:

#ifndef GAME_H_
#define GAME_H_

#define COLUMNS 15
#define ROWS 15

int board[ROWS][COLUMNS];

#endif /* GAME_H_ */

That expression is not allowed, it can be used in C++, the only way around this is to define it like this:

#ifndef GAME_H_
#define GAME_H_

#define COLUMNS 15
#define ROWS 15

int board[ROWS][COLUMNS];

#endif /* GAME_H_ */
习惯成性 2024-10-07 19:51:18

从 C99 开始,您可以声明所谓的可变长度数组 (VLA),其中数组维度的大小是一个非常量表达式; IOW,您可以这样做:

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

请注意,这仅适用于 C99; C89 及更早版本要求您对数组维度使用编译时常量表达式。

VLA 的问题在于,由于它们的工作方式,它们只能在块范围内声明(即在函数内或函数中的复合语句内);它们可能不会被声明为staticextern,并且它们可能不会在文件范围内声明(这是特定错误消息的来源)。

在这种特殊情况下,您将需要使用编译时常量表达式(const 限定的变量不是):

#define COLUMNS 15
#define ROWS    15

extern int board[ROWS][COLUMNS];

请注意添加 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:

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

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 or extern, 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):

#define COLUMNS 15
#define ROWS    15

extern int board[ROWS][COLUMNS];

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 for board and it will be up to the linker to sort it all out.

远山浅 2024-10-07 19:51:18
int board[rows][columns];

无效 C。只能用常量定义数组,而不能用变量引用定义数组。即使变量引用引用的是常量值,行和列也是对常量值的引用,而不是常量值本身。

有多种方法可以使其按照您的意愿工作:

  1. 您可以在预处理器中定义“变量”,以便它们在编译之前被扁平化为常量。
  2. 您可以将这些值定义为枚举 enum 的一部分,因为 C 编译器的枚举规则是在检测到类型不匹配时自动将它们转换为常量值。
int board[rows][columns];

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:

  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.
花开半夏魅人心 2024-10-07 19:51:18

数组声明在编译前需要一个常量值。您可以在其中#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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文