如何修复“可变长度数组”折叠为常数数组作为扩展名。没有有问题的代码(由于作业规则)而没有更改?

发布于 2025-02-09 14:06:37 字数 845 浏览 1 评论 0原文

我正在从事C中的任务,这使我发疯。我必须在预编码程序上填写一些功能,并且不允许更改它。但是,一切都很好,但是,由于几天前,由于以下错误,我开始无法编译它:

错误:变量长度数组折叠成恒定数组作为扩展

,它将我指向我不允许更改的代码线,我都不记得更改。奇怪的是,我不记得我最后一次更改了它,然后才开始抛出错误(我在上次更改后两天就试图将其汇编,一旦打开代码,我就试图将其编译)。

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// function declaration
int free_linked_list(node *n);


// TODO: Choose number of buckets in hash table
const unsigned int N = 26;

// Hash table
node *table[N];

这是我认为相关的代码,取自CS50X PSET5。错误是在最后一行(node *table [n];)上丢弃的。除了函数声明“ Free_linked_list”之外,所有这些代码都随附作业,这意味着我不能更改任何一个(除了分配给n的数字,我恢复了此问题的初始值),我不认为我做过。我想我的问题是,我是否有可能在代码中做其他事情,这会使编译器在此行中丢弃此错误?我什至不确定我理解错误本身(在堆栈溢出中有一个问题,但是我真的无法理解答案,而解决方案通过了我无法完成此任务的更改代码)。任何帮助将不胜感激。请让我知道发布更多代码是否有帮助。

谢谢!!

I am working on an assignment in C that is driving me crazy. I have to fill some functions out on a pre-coded program, and I am not allowed to change it. Everything was going fine, however, since a few days ago, I started to not be able to compile it because of the following error:

error: variable length array folded to constant array as an extension

and it points me to a line of code that I am not allowed to change, neither I remember changing. Weirdly enough, I cannot remember what I changed last before it started throwing me the error (I tried to compile it as soon as I turned the code on, two days after I had last changed it).

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// function declaration
int free_linked_list(node *n);


// TODO: Choose number of buckets in hash table
const unsigned int N = 26;

// Hash table
node *table[N];

This is the code that I think is relevant, taken from CS50x pset5. The error is thrown about the very last line (node *table[N];). All this code, except the function declaration "free_linked_list", came with the assignment, which means I cannot change any of it (except the number assigned to N, which I reverted to its initial value for this question), and I dont think I did. I guess my question is, is it possible that I have done something else in my code that would make the compiler throw this error in this line?? I am not even 100% sure I understand the error itself (there is one question about it in stack overflow, but I couldnt really understand the answer, and the solution passed through altering code that I cant for this assignment). Any help would be greatly appreciated. Please let me know if it would be helpful to post more code.

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

初心未许 2025-02-16 14:06:37

在标准C, 可变长度阵列中,由于C17 6.7.6.2/2(数组声明器)的约束,因此不允许在功能之外使用变量:

[...]如果将标识符声明为具有静态或线程存储持续时间的对象,则不得具有可变长度阵列类型。

节点 *table [n];在OP代码中具有静态存储持续时间,因为它是在函数之外定义的,并且它是 act 可变长度阵列,因为它的大小是如何的指定的。

数组类型是否是可变长度阵列类型归结为指定大小的方式。来自C17 6.7.6.2/4(数组声明器):

如果不存在大小,则数组类型是不完整的类型。如果大小为 * 而不是表达式,则数组类型为可变长度阵列未指定的大小,只能使用在声明或带有函数原型范围的名称中; 145)此类数组仍然是完整的类型。如果大小是整数常数表达式,并且元素类型具有已知常数大小,则数组类型不是可变长度阵列类型。否则,数组类型为可变长度阵列类型。 (可变长度阵列是实现不需要支持的条件功能;请参阅
6.10.8.3。)

In OP's provided code, the size is N, but it is not an integer constant expression (see below) even though the variable N< /code>用const预选赛完全定义。因此,node *table [n];具有可变长度数组类型。

整数恒定表达式由C17 6.6/6(常数表达式)定义,如下:

an 整数常数表达式 119)应具有整数类型,并且只有整数常数,枚举常数,字符常数, sizeof sizeof 表达式的结果是整数常数, _Alignof 表达式以及浮动常数是铸件的直接操作数。整数常数表达式中的铸造运算符仅应将算术类型转换为整数类型,除了作为操作数的一部分到 sizef _alignof 操作员。


特别是,变量(即使是声明的const)不满足上述定义整数常数expression

C17 6.6/10中有一个伸展条款:

实现可以接受其他形式的恒定表达式。

据推测,这是编译器警告的扩展程序,请注意OP的node *table [n];代码。 (Clang treats this as a warning, not an error, unless warnings are promoted to errors by the -Werror option.)

An acceptable way to fix the problem would be to specify the size with a macro instead of使用const unsigned int变量。 Eg:

#define N 26

But apparently OP is not allowed to change that. OP应向他们的讲师寻求澄清,并在提供的代码中指出问题。 [编辑2023-03-08-根据 @nigh_anxiety的评论,允许OP替换const unsigned int n = 26;#define n 26,它将整齐地求解他们的问题。]

In standard C, variable length array variables are not allowed outside of functions due to this constraint from C17 6.7.6.2/2 (Array declarators):

[...] If an identifier is declared to be an object with static or thread storage duration, it shall not have a variable length array type.

The node *table[N]; in OP's code has static storage duration because it is defined outside a function, and it is a variable length array because of how its size is specified.

Whether or not an array type is a variable length array type boils down to how the size is specified. From C17 6.7.6.2/4 (Array declarators):

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;145) such arrays are nonetheless complete types. 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; 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.)

In OP's provided code, the size is N, but it is not an integer constant expression (see below) even though the variable N is fully defined with a const qualifier. Therefore, node *table[N]; has a variable length array type.

An integer constant expression is defined by C17 6.6/6 (Constant expressions) as follows:

An integer constant expression119) 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.

In particular, variables (even those declared const) do not satisfy the above definition of an integer constant expression.

There is a get-out clause for extensions in C17 6.6/10:

An implementation may accept other forms of constant expressions.

Presumably, this is the extension that the compiler is warning about for OP's node *table[N]; code. (Clang treats this as a warning, not an error, unless warnings are promoted to errors by the -Werror option.)

An acceptable way to fix the problem would be to specify the size with a macro instead of with a const unsigned int variable. E.g.:

#define N 26

But apparently OP is not allowed to change that. OP should seek clarification from their instructor, pointing out the problem in the supplied code. [EDIT 2023-03-08 - according to @nigh_anxiety's comment, OP is allowed to replace const unsigned int N = 26; with #define N 26, which would neatly solve their problem.]

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