Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing pointer to 2D array c++

I'm having this problem for quite a long time - I have fixed sized 2D array as a class member.

class myClass
{ 
public:
        void getpointeM(...??????...);
        double * retpointM();

private:

   double M[3][3];

};

int main()
{
     myClass moo;
     double *A[3][3];

     moo.getpointM( A );  ???
     A = moo.retpointM(); ???

} 

I'd like to pass pointer to M matrix outside. It's probably very simple, but I just can't find the proper combination of & and * etc.

Thanks for help.

like image 343
Moomin Avatar asked Feb 22 '10 04:02

Moomin


1 Answers

double *A[3][3]; is a 2-dimensional array of double *s. You want double (*A)[3][3]; .

Then, note that A and *A and **A all have the same address, just different types.

Making a typedef can simplify things:

typedef double d3x3[3][3];

This being C++, you should pass the variable by reference, not pointer:

void getpointeM( d3x3 &matrix );

Now you don't need to use parens in type names, and the compiler makes sure you're passing an array of the correct size.

like image 100
Potatoswatter Avatar answered Sep 18 '22 05:09

Potatoswatter