Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question about 2d matrix and pointer in the function definition

Reply
Thread Tools

question about 2d matrix and pointer in the function definition

 
 
Yudan Yi \(OSU\)
Guest
Posts: n/a
 
      06-11-2005
Hi
I define a function, such as
void matrix_multi(double **a, double **b, double **c, int n, int m, int q);
then when I called this function, I must first declare
double **a, double **b, double **c;
My question: is there any way to call the function when I declare
double a[5][10], b[10][5],c[5][5];
matrix_multi(a, b, c, 5, 10, 5); // => will give error message, what should
I do?
Thanks
Yudan



 
Reply With Quote
 
 
 
 
James Daughtry
Guest
Posts: n/a
 
      06-11-2005
A two dimensional array doesn't evaluate to a pointer to a pointer, it
evaluates to a pointer to an array of size N, so

double a[5][10];

would require one of two function parameter declarations:

void foo(double arg[5][10]);
or
void foo(double (*arg)[10]);

If you need the second dimension to be variant, then you're SOL unless
you allocate memory to a pointer to a pointer and simulate a two
dimensional array. Alternatively, you could use a container such as
std::vector:

#include <vector>

void foo(const std::vector<std::vector<double> >& arg);

std::vector<std::vector<double> > a(5, std::vector<double>(10));
foo(a);

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      06-11-2005
James Daughtry wrote:

> A two dimensional array doesn't evaluate to a pointer to a pointer, it
> evaluates to a pointer to an array of size N, so
>
> double a[5][10];
>
> would require one of two function parameter declarations:
>
> void foo(double arg[5][10]);
> or
> void foo(double (*arg)[10]);


A third version would be:

void foo(double (&arg)[5][10]);

But that would make both dimensions fixed.

> If you need the second dimension to be variant, then you're SOL unless
> you allocate memory to a pointer to a pointer and simulate a two
> dimensional array.


Or make a one dimensional array and do the index calculation yourself.

> Alternatively, you could use a container such as std::vector:
>
> #include <vector>
>
> void foo(const std::vector<std::vector<double> >& arg);
>
> std::vector<std::vector<double> > a(5, std::vector<double>(10));
> foo(a);


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initialization of a const matrix implemented as pointer-to-pointer Andrea Taverna (Tavs) C Programming 17 09-24-2008 12:28 PM
Matrix*Vector and Vector*Matrix Holgerson C++ 3 10-26-2007 07:38 AM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
function pointer and member function pointer question glen stark C++ 2 10-10-2003 01:41 PM
Re: Matrix DTS and Matrix 2 DTS? PeterTHX DVD Video 0 08-03-2003 05:46 AM



Advertisments