C固定大小的数组被视为可变大小

C固定大小的数组被视为可变大小

本文介绍了C固定大小的数组被视为可变大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图定义一个静态数组,其大小在编译时就应该知道(这是一个常量表达式)。当gcc包含浮点常量时,似乎无法确定数组的大小(并且我得到 ...的存储大小不是恒定的)。

I have been trying to define a static array with size that should be known at compile time (it's a constant expression). It appears that gcc cannot determine the size of the array when it contains a floating point constant (and I get "storage size of ... isn’t constant").

这是一个最小的示例:

int main(void)
{
    static int foo[(unsigned)(2 / 0.5)];
    return 0;
}

此行为的原因是什么?

编辑

我已经有了所需的答案。我仍然不理解不允许使用这种表达方式的原因,但这是一个单独的问题。
我会很好奇地解释我是如何解决这个问题的。

I already have the answer I needed. I still don't understand the rationale behind not allowing that kind of expressions, but this is a separate question.I'll explain for the curious how I arrived at the problem.

这是关于我正在做练习的游戏。部队在战场上移动,我将移动分为多个步骤。我必须记住每个步骤中每个单元的位置,以便以后显示动画。选择步数是为了确保有一个步,在此步上,各单元应足够接近以相互对抗,但不能接近以至于发生碰撞。以下是相关的代码段:

It's about a game I'm writing as an excercise. Units move on a battlefield and I have divided the movement in steps. I have to remember the position of each unit on each step so that I can display animation later. The number of steps is chosen so that it ensures there will be a step on which units are close enough to fight each other but not so close as to collide. Here are the relevant pieces of code:

#define UNIT_SPEED_LIMIT 12
#define DISTANCE_MELEE 0.25
#define MOVEMENT_STEPS (unsigned)(2 * UNIT_SPEED_LIMIT / DISTANCE_MELEE)

struct position (*movements)[MOVEMENT_STEPS + 1];

定义 DISTANCE_MELEE (最大距离可以进行战斗),并且使用它来计算步数似乎是一种自然的处理方式(更多的是,因为我在多个上下文中使用了此常数)。由于无法以这种方式定义运动,因此我必须发明一个概念,例如单个距离单位的步数,并使用乘以 int 而不是除以 double 。我想避免动态内存分配,以使代码保持简单。

Defining DISTANCE_MELEE (maximum distance at which close combat is possible) and using it to calculate the number of steps seems to be the natural way to proceed (more so because I use this constant in multiple contexts). Since I cannot define movements this way, I have to invent a concept like "number of steps for a single unit of distance" and use multiplication by int instead of division by double. I want to avoid dynamic memory allocation in order to keep the code simple.

推荐答案

根据公开的C99草案标准n1256,数组声明的语法由

According to the publicly available C99 draft standard n1256, the syntax for array declaration is described by

描述2

4


所以 [] 必须是整数常量表达式,数组才能声明为静态的存储时间。该标准对整数常量表达式有这样的说法:

So the expression in the [] must be an integer constant expression for the array to be declarable with static storage duration. The standard has this to say about integer constant expressions:

6


不幸的是,(未签名)(2 / 0.5)不能立即应用强制转换 为浮点常量,而不是算术常量表达式。这并不构成整数常量表达式,因此,对于具有静态存储持续时间的数组的大小,这是不允许的。

Unfortunately, (unsigned)(2 / 0.5) does not apply the cast immediately to a floating-point constant, but rather to an arithmetic constant expression. This does not constitute an integer constant expression, and is thus not permissible as the size of an array with static storage duration.

这篇关于C固定大小的数组被视为可变大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:52