T
Tom Page
Hello all
I have a question that may or may not be simple - I need my three
dimensional array to be of the form:
array[jj][kk][ii]
where jj and kk range from 0 to some constant fixed at the very beginnning
of the program (currently 20) but [ii] goes from 0 to some boundary that
depends on calculations performed within the program. I know it is possible
to do this:
double (*somearray)[fixed][fixed]= new double[variable
expression][fixed][fixed]
but can I have the last subscript as variable? - this is not just a question
of hypothetics, but a need to have the data in this format as an external
procedure expects the data with the first and second dimensions to be one
type of data (which has a fixed size) and the third dimension to have a
variable size.
Many thanks for any help!
Currently I do this:
const int griddef=20;
int somefunction(unsigned int wldepth,other arguments...)
int status, ii, jj, kk;
double *array[griddef][wldepth]; /*<------This doesn't work as wldepth is a
variable, but it shows what I want to do!*/
long naxes[3] = { griddef, griddef,wldepth };
array[0][0] = (double *)malloc( naxes[0] * naxes[1] * naxes[2] * sizeof(
double ) );
for(jj=0;jj<griddef;jj++)
{
for(ii=0;ii<griddef;ii++)
{
array[jj][ii] = array[0][0] + griddef*(griddef*jj+ii);
}
}
for(kk=0;kk<griddef;kk++)
{
for (jj=0;jj<griddef;jj++)
{
for (ii=0;ii<wldepth;ii++)
{
array[jj][kk][ii] = some calculation based on ii,jj and kk;
}
}
}
return 0;
}
I have a question that may or may not be simple - I need my three
dimensional array to be of the form:
array[jj][kk][ii]
where jj and kk range from 0 to some constant fixed at the very beginnning
of the program (currently 20) but [ii] goes from 0 to some boundary that
depends on calculations performed within the program. I know it is possible
to do this:
double (*somearray)[fixed][fixed]= new double[variable
expression][fixed][fixed]
but can I have the last subscript as variable? - this is not just a question
of hypothetics, but a need to have the data in this format as an external
procedure expects the data with the first and second dimensions to be one
type of data (which has a fixed size) and the third dimension to have a
variable size.
Many thanks for any help!
Currently I do this:
const int griddef=20;
int somefunction(unsigned int wldepth,other arguments...)
int status, ii, jj, kk;
double *array[griddef][wldepth]; /*<------This doesn't work as wldepth is a
variable, but it shows what I want to do!*/
long naxes[3] = { griddef, griddef,wldepth };
array[0][0] = (double *)malloc( naxes[0] * naxes[1] * naxes[2] * sizeof(
double ) );
for(jj=0;jj<griddef;jj++)
{
for(ii=0;ii<griddef;ii++)
{
array[jj][ii] = array[0][0] + griddef*(griddef*jj+ii);
}
}
for(kk=0;kk<griddef;kk++)
{
for (jj=0;jj<griddef;jj++)
{
for (ii=0;ii<wldepth;ii++)
{
array[jj][kk][ii] = some calculation based on ii,jj and kk;
}
}
}
return 0;
}