TLDR: I want to find a way to globally declare dynamically allocatable arrays which cannot be declared as just constant arrays due to the need to change the length of the arrays, depending on the number of equations, but most likely not the array data. These arrays need to be accessible by various functions contained in source files making up the program I'm writing.
I'm currently porting a program from Fortran to C and I want to declare some global variables for the entire program, which consists of several .c
and .h
files, so that the said variables could be used not only in the file where they are declared, but in other files as well. [Actually they are "semi-variables" because what will change is the length of the arrays, which depends on the number of equations, the real aim of this question is to declare arrays that are to contain constant data, but that data can be contained in more array slots, so the arrays must be dynamically allocatable.] The problem is that these variables are dynamically allocatable arrays and these arrays have to be filled with some numbers.
This is how I declare the variables:
in the file problemsize.h
:
extern double *GammaL;
in the file problemsize.c
:
double *GammaL;
Then I try to create a function to fill the arrays by changing their values in place. The function would be called from some other .c
files which #include problemsize.h
. Now, I know that one could create a function like this:
void fill_array(double **GammaL, double **GammaR, double **GammaT, ...) {
GammaL = malloc(arrayLength*sizeof(double));
for (int i=0; i<arrayLength; i++) {
(*GammaL)[i] = 0.1;
}
GammaR = malloc(arrayLength*sizeof(double));
for (int i=0; i<arrayLength; i++) {
(*GammaR)[i] = 0.2;
}
[...] // And so on
}
and then invoke this function from external .c
files. But the thing is that I have many such arrays that would have to be passed as arguments to this function. I'm wondering if it is possible to have a function like the above to do the same job (change the arrays in place) without the need to invoke it with these arrays being the arguments?
Or is there some other solution that would solve this problem?
Edit: I had the impression that in C one could only change pointers in place by passing them as parameters to a function. Now, after some clarifications, it is clear that global variables can be changed by void functions without passing any parameters, which definitely makes sense.
Now, after some clarifications, it is clear that global variables can be changed by void functions without passing any parameters, which definitely makes sense.
The void keyword can be used as a type specifier to indicate the absence of a type or as a function's parameter list to indicate that the function takes no parameters. When used as a type specifier, it is most often used as a function return type to indicate that the function does not return a value.
A void function with value parameters are declared by enclosing the list of types for the parameter list in the parentheses. To activate a void function with value parameters, we specify the name of the function and provide the actual arguments enclosed in parentheses.
You use void as the return type of a method (or a local function) to specify that the method doesn't return a value. You can also use void as a referent type to declare a pointer to an unknown type. For more information, see Pointer types. You cannot use void as the type of a variable.
If your arrays are global, then there is no need to pass them as parameters. Any function can use them without any problem, and modify their values.
In short, this is what you can do in problemsize.c
:
#define ARR_SIZE 4
double GammaL[ARR_SIZE];
double GammaR[ARR_SIZE];
double GammaT[ARR_SIZE];
void initialize_arrays(void) {
size_t i;
for (i = 0; i < ARR_SIZE; i++) {
GammaL[i] = ...;
GammaR[i] = ...;
GammaT[i] = ...;
}
}
Or, if your arrays don't have a fixed size:
size_t arr_size = 0;
double *GammaL;
double *GammaR;
double *GammaT;
void initialize_arrays(void) {
size_t i;
arr_size = /*... calculate somehow ... */;
GammaL = malloc(arr_size * sizeof(double));
GammaR = malloc(arr_size * sizeof(double));
GammaT = malloc(arr_size * sizeof(double));
for (i = 0; i < arr_size; i++) {
GammaL[i] = ...;
GammaR[i] = ...;
GammaT[i] = ...;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With