简体   繁体   中英

Global Variable Array in C

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?

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.

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.

Do the initialization in a function (eg in main()):

int main() {
    Array = malloc(dim1 * dim2 * sizeof(double));
    ...
}
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
$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM