Pointers and multi dimensional arrays
Hi,
I have troubles understanding the relationship between pointers (to double) and multidimensional arrays (of doubles).
I have a C++ routine that returns pointers to doubles, and a class object that I'm creating computes multidimensional arrays, because I like to think of the data as matrices.
Then when the computation is done all I have to do is to copy the array values into the location pointed by the pointer, increment it and so on.
So this is what I have:
Code:
void STDCALL FIESUB ( double *dfddis, double *dfdvel )
{
...
}
In this routine I instantiate an object of the class Field:
Code:
class msField
{
private:
double m_dfdx[6][6];
double m_dfdxdot[6][6];
public:
void getForceDerivatives(double *fddist, double *fdvel);
};
The idea being that in getForceDerivatives I would do something like this:
Code:
void msField::getForceDerivatives(double *fddist, double *fdvel)
{
// retrieves the force derivatives from the field member datas
int i, j;
for ( i = 0; i < 6; i++)
{
for ( j = 0; j < 6; j++)
{
*fddist++=m_dfdx[i][j];
*fdvel++=m_dfdxdot[i][j];
}
}
}
That function is called like this (inside of FIESUB):
Code:
rField.getForceDerivatives(dfddis, dfdvel);
The result?
Mayhem.
Obviously I am forgetting something.
Can anyone explain the relationship between pointers to double and arrays of doubles (multidimensional arrays, that is...).
Any help much appreciated!!
And72.
re: pointers and multidimensional arrays
Hi and thanks for your answer.
The problem is that I am writing an extension: in other words I do not control upstream from the routine in question.
So by mayhem I mean that the main application (in development) is getting a bunch of NAN (not a number?) as a consequence of my assignment.
Now looking at the code i suspect that I am doing something wrong in the computation, not in the assignment.
I trust that whoever wrote the main application reserved enough memory for storing 36 values, but I will double check that.
Lots of blathering on my part, but thanks for double checking my code.
And72.