Hi again, I am lost about how the memory is allocated by using the syntax explained in this thread.

Specifically, to give a summary, I tried to allocate memory in such a way that would be EQUIVALENT to a static allocation given by the following:

Code:

#define NX 5
#define NY 4
float zonal[NY-1][NX-1];
float *variables[] = {(float*)zonal}

but, since the number of entries for variables can change during execution (I may not only have zonal, but also other variables, and the number of this variables will be known only at a certain point of the code), I tried to do the following:

Code:

#define NX 5
#define NY 4
int i,j,k;
float zonal[NY-1][NX-1];
int nvars; //unknown at this time
float **variables;
//Dyn. Allocation (done in 2 steps):
nvars = 1; //this number can change! that is why I need dynamic allocation
variables = (float**) malloc(nvars* sizeof(float));
for(i=0; i<nvars; i++){
variables[i] = (float*) malloc( NX*NY * sizeof(float));
//Assign zonal[][] simply as a 2D array, e.g.:
for(j = 0; j < NY-1; ++j)
for(i = 0; i < NX-1; ++i, ++index)
zonal[j][i] = (float)(i+j);
//And I finally assigned (tried to) "variables[][]" with
k=0;
for(j = 0; j < NY; ++j){
for(i = 0; i < NX; ++i){
variables[0][k] = zonal[j][i];
k++;
}
}

but this last assignment results to be wrong, in fact, it assigns variables with the wrong numbers in the wrong position.

How is this last assignment supposed to be done in order to be equivalent to the static version:

Code:

float *variables[] = {(float*)zonal}

?

Thank you very much again

CFD