multidimensional array function - simple question
Hi, I'm not an expert programmer and I'm having trouble with a function I am trying to create. I have the following code for a multidimensional array.
Code:
int memory_4D(){
double**** array_4D;
int i, j, k;
array_4D = (double****)malloc(NZ*sizeof(double***));
for(k=0; k<NZ; k++) {
array_4D[k] = (double***)malloc(NY*sizeof(double**));
}
for(k=0; k<NZ; k++) {
for(j=0; j<NY; j++) {
array_4D[k][j] = (double**)malloc(NX*sizeof(double*));
}
}
for(k=0; k<NZ; k++) {
for(j=0; j<NY; j++) {
for(i=0; i<NX; i++) {
array_4D[k][j][i] = (double*)malloc(N*sizeof(double));
}
}
}
return 0;
}
I am trying to create the function memory_4D that I can call to allocate memory to all of my 4D arrays. For eaxmple, I have the arrays double**** f, double**** g, and double h**** that I want to allocate memory for. I currently have the above code typed three different times with f, g, and h replacing array_4D. I know I can create the function memory_4D that I call and assign f, g, and h with the appropriate memeory. I have tried several ways of initializing and calling memory _4D and none seem to work. I'm sure this is an easy questions. :confused: Does anyone have a suggestion?