C 中的全局变量数组
我正在尝试创建一个全局数组,其大小由运行时的外部参数文件确定。
我已经看到了关于此的其他问题并尝试过:
int const Nt=1280;
double *Array = NULL;
Array = malloc(Nt * Nt * sizeof(double));
但是,我收到了错误,例如:
错误:数组类型冲突
错误:初始值设定项元素不是常量
如何创建这样的全局数组,而无需每次需要更改时重新编译它的大小?
I'm trying to create a global array whose size is determined by an external parameter file at runtime.
I've seen other questions on this and tried:
int const Nt=1280;
double *Array = NULL;
Array = malloc(Nt * Nt * sizeof(double));
However, I get errors such as:
Error: Conflicting types for Array
Error: Initializer element is not constant
How can I create such a global array without needing to recompile every time I need to change its size?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
全局范围内不允许赋值。您必须在函数中执行此操作。
假设以上 2 个语句是在全局范围内。它们是初始化的示例,因为语句在声明本身处赋值。这在全局范围内是允许的,并且初始值设定项可以是常量值、字符串文字或此时可访问的其他变量。
类似地,在您的示例中
Array
只是一个声明。在函数中进行分配。关于分段错误,请发布更多代码。
Assignment is not allowed at global scope. You have to do it in a function instead.
Assuming the above 2 statements are at global scope. They are examples of initialization because the statements assign value at the declaration itself. This is allowed at global scope and the initializers can be constant values, string literals or other variables accessible at this point.
And similarly in your example
Array
is just a declaration. Do the assignment in a function.And regarding the segmentation fault, post more code.
全局函数初始值设定项必须直接编译到可执行文件中。因此,它们不能包含任何在运行时更改的信息,也不能包含函数调用。并且初始值设定项必须与声明位于同一语句中。
有效:
无效:
如果您无法将初始化程序放入这些规则中,则应该将其移至函数中。
Global function initializers have to be compiled directly into the executable. Therefore, they cannot contain any information that changes at runtime, and they cannot contain function calls. And the initializers must be in the same statement as the declaration.
WORKS:
DOESN'T WORK:
You should move your initializer into a function if you can't fit it within those rules.
在函数中进行初始化(例如在 main() 中):
Do the initialization in a function (e.g. in main()):
这很好(只要您在编写之前包含了定义 NULL 的标头)。
你说这会产生:
这是因为编译器正在竭尽全力接受您的代码,并设法将
Array
解释为“隐式int<” /code>' 变量,就像您写的那样:
由于
int
与double *
不同,它会给您带来冲突类型错误。第二条消息告诉您初始化程序 - 对
malloc()
的调用 - 不是常量,函数外部的初始化程序必须是常量。如果您将该行放在函数内,那么您将有一个赋值(而不是初始值设定项),并且代码会没问题,但最好写为:您应该使用更严格的编译警告进行编译。我得到:
This is fine (as long as you've included a header that defines NULL before you write it).
You say this yields:
That is because the compiler is bending over backwards to accept your code, and manages to interpret the
Array
as an 'implicitint
' variable, as if you wrote:Since
int
is not the same asdouble *
, it gives you the conflicting types error.The second message tells you that the initializer - the call to
malloc()
- is not a constant, and initializers outside functions must be constants. If you placed the line inside a function, then you'd have an assignment (not an initializer), and the code would be OK, though better written as:You should be compiling with more stringent compilation warnings. I get: