无法在 DevC 中的语句之后声明变量
这里的问题是,在函数中已经有一些语句之后,我无法在函数内声明变量。 在开始时声明工作正常,但在某些事情之后,它会给出解析错误。 例如:
int main()
{
int b;
b = sisesta();
float st[b];
return 0;
}
我想声明一个数组 st
,其大小由另一个函数返回,但它不允许我这样做! 说“浮动之前解析错误”。 顺便说一句,这是用 C 语言编写的,但我猜它与具有相同语法的其他语言中的内容相同。
任何帮助表示赞赏。
The trouble here is that I can't declare variables inside a function after the function already has some statements in it. Declaring at the start works fine, but after something, it gives a parse error. For example:
int main()
{
int b;
b = sisesta();
float st[b];
return 0;
}
I'd like to declare an array st
with its size being returned by another function, but it won't let me do it! Says "Parse error before float". This is in C by the way, but I guess its identical to what it would be in other languages with the same syntax.
Any help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 C99 之前的 C 标准中,必须在函数的开头声明局部变量。 从 C99 开始,不再需要这样做。
由于 Dev-C++ 附带了 gcc,并且最近的 gcc 版本确实部分支持 C99,因此您可以尝试将
-std=c99
添加到 Dev-C++ 设置中的 gcc 参数列表中以触发 C99 模式。In C standards before C99, you have to declare your local variables at the beginning of the function. Beginning with C99, this is no longer required.
Since Dev-C++ ships with gcc and recent gcc versions do support C99 partially, you can try adding
-std=c99
to the gcc argument list in the Dev-C++ settings to trigger C99 mode.C 语言中的老兄,你必须在开始时声明所有变量。 您不能在语句之间声明
Dude in C you have to declare all variables at the start. You can't declare between statements
你可以
malloc()
将float*
设置为您想要的大小(只需记住free()
之后):you could
malloc()
afloat*
to the size you want (just remember tofree()
it afterwards):事实证明,我只是有一个旧版本的 DevC++,它不支持较新的标准,在最新版本中,语句工作正常,无论如何,感谢您的帮助。
It turns out that I just had an old version of DevC++ which didnt support the newer standard, with the latest release the statements work fine, thanks for the help anyway.
即使在 C89 中,在函数开头进行所有声明也只是一种风格选择 - 您在代码中遇到的麻烦是您试图在未知大小的堆栈上声明一个数组,而这是不允许的直到C99。 如果您要执行相同的代码,但将“float st[b]”替换为“b”为常量的语句,那么它会起作用,就像“float st[10]”
Even in C89, it was just a stylistic choice to do all declarations at the beginning of a function - the trouble that you hit in your code was that you attempted to declare an array on the stack of an unknown size, and that was not allowed until C99. If you were to do the same code, but replace "float st[b]" with a statement where "b" was constant, it would work, like "float st[10]"