如何修复“可变长度数组”折叠为常数数组作为扩展名。没有有问题的代码(由于作业规则)而没有更改?
我正在从事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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在标准C, 可变长度阵列中,由于C17 6.7.6.2/2(数组声明器)的约束,因此不允许在功能之外使用变量:
节点 *table [n];
在OP代码中具有静态存储持续时间,因为它是在函数之外定义的,并且它是 act 可变长度阵列,因为它的大小是如何的指定的。数组类型是否是可变长度阵列类型归结为指定大小的方式。来自C17 6.7.6.2/4(数组声明器):
In OP's provided code, the size is
N
, but it is not an integer constant expression (see below) even though the variableN< /code>用
const
预选赛完全定义。因此,node *table [n];
具有可变长度数组类型。整数恒定表达式由C17 6.6/6(常数表达式)定义,如下:
特别是,变量(即使是声明的
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: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):
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):
In OP's provided code, the size is
N
, but it is not an integer constant expression (see below) even though the variableN
is fully defined with aconst
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:
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:
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.: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.]