C 中的全局变量数组

发布于 2024-12-02 13:53:57 字数 279 浏览 1 评论 0原文

我正在尝试创建一个全局数组,其大小由运行时的外部参数文件确定。

我已经看到了关于此的其他问题并尝试过:

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 技术交流群。

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

发布评论

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

评论(4

锦上情书 2024-12-09 13:53:57

全局范围内不允许赋值。您必须在函数中执行此操作。

int const Nt = 1280;
double *Array = NULL;

假设以上 2 个语句是在全局范围内。它们是初始化的示例,因为语句在声明本身处赋值。这在全局范围内是允许的,并且初始值设定项可以是常量值、字符串文字或此时可访问的其他变量。

const int Nt ; // Just declaration
Nt =  1280 ;   // Error. Assignment is not allowed at global scope.

const char* myName = "CHP" ;
int a[] = { 11,22,33,44,55 } ;

类似地,在您的示例中 Array 只是一个声明。在函数中进行分配。

void assignArray() {

    Array = malloc(Nt * Nt * sizeof(double));

    // ...

    printf("%d", sizeof(Array)/sizeof(Array[0]); // Size which is nothing but Nt*Nt
                                                 // 0 to (Nt * Nt) - 1
}

关于分段错误,请发布更多代码。

Assignment is not allowed at global scope. You have to do it in a function instead.

int const Nt = 1280;
double *Array = NULL;

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.

const int Nt ; // Just declaration
Nt =  1280 ;   // Error. Assignment is not allowed at global scope.

const char* myName = "CHP" ;
int a[] = { 11,22,33,44,55 } ;

And similarly in your example Array is just a declaration. Do the assignment in a function.

void assignArray() {

    Array = malloc(Nt * Nt * sizeof(double));

    // ...

    printf("%d", sizeof(Array)/sizeof(Array[0]); // Size which is nothing but Nt*Nt
                                                 // 0 to (Nt * Nt) - 1
}

And regarding the segmentation fault, post more code.

思念满溢 2024-12-09 13:53:57

全局函数初始值设定项必须直接编译到可执行文件中。因此,它们不能包含任何在运行时更改的信息,也不能包含函数调用。并且初始值设定项必须与声明位于同一语句中。

有效:

int x = 1;

无效:

int x;
x = 1;

如果您无法将初始化程序放入这些规则中,则应该将其移至函数中。

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:

int x = 1;

DOESN'T WORK:

int x;
x = 1;

You should move your initializer into a function if you can't fit it within those rules.

桃扇骨 2024-12-09 13:53:57

在函数中进行初始化(例如在 main() 中):

int main() {
    Array = malloc(dim1 * dim2 * sizeof(double));
    ...
}

Do the initialization in a function (e.g. in main()):

int main() {
    Array = malloc(dim1 * dim2 * sizeof(double));
    ...
}
花间憩 2024-12-09 13:53:57
double *Array = NULL;

这很好(只要您在编写之前包含了定义 NULL 的标头)。

Array = malloc(dim1 * dim2 * sizeof(double));

你说这会产生:

错误:数组类型冲突
错误:初始化元素不是常量

这是因为编译器正在竭尽全力接受您的代码,并设法将 Array 解释为“隐式 int<” /code>' 变量,就像您写的那样:

int Array = ...

由于 intdouble * 不同,它会给您带来冲突类型错误。

第二条消息告诉您初始化程序 - 对 malloc() 的调用 - 不是常量,函数外部的初始化程序必须是常量。如果您将该行放在函数内,那么您将有一个赋值(而不是初始值设定项),并且代码会没问题,但最好写为:

if (Array == NULL)
    Array = malloc(dim1 * dim2 * sizeof(double));

您应该使用更严格的编译警告进行编译。我得到:

$ cat x.c
#include <stdlib.h>
double *Array = NULL;
Array = malloc(23 * 37 * sizeof(double));
$ gcc -g -std=c99 -pedantic -Wall -Werror -c x.c
x.c:3:1: error: data definition has no type or storage class [-Werror]
x.c:3:1: error: type defaults to ‘int’ in declaration of ‘Array’ [-Werror]
x.c:3:1: error: conflicting types for ‘Array’
x.c:2:9: note: previous definition of ‘Array’ was here
x.c:3:9: error: initialization makes integer from pointer without a cast [-Werror]
x.c:3:1: error: initializer element is not constant
cc1: all warnings being treated as errors
$
double *Array = NULL;

This is fine (as long as you've included a header that defines NULL before you write it).

Array = malloc(dim1 * dim2 * sizeof(double));

You say this yields:

Error: Conflicting types for Array
Error: Initializer element is not constant

That is because the compiler is bending over backwards to accept your code, and manages to interpret the Array as an 'implicit int' variable, as if you wrote:

int Array = ...

Since int is not the same as double *, 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:

if (Array == NULL)
    Array = malloc(dim1 * dim2 * sizeof(double));

You should be compiling with more stringent compilation warnings. I get:

$ cat x.c
#include <stdlib.h>
double *Array = NULL;
Array = malloc(23 * 37 * sizeof(double));
$ gcc -g -std=c99 -pedantic -Wall -Werror -c x.c
x.c:3:1: error: data definition has no type or storage class [-Werror]
x.c:3:1: error: type defaults to ‘int’ in declaration of ‘Array’ [-Werror]
x.c:3:1: error: conflicting types for ‘Array’
x.c:2:9: note: previous definition of ‘Array’ was here
x.c:3:9: error: initialization makes integer from pointer without a cast [-Werror]
x.c:3:1: error: initializer element is not constant
cc1: all warnings being treated as errors
$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文