question about 2d matrix and pointer in the function definition



 DEVELOP > c-Plus-Plus > question about 2d matrix and pointer in the function definition

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Yudan Yi \OSU"
Date: 11 Jun 2005 08:55:21 AM
Object: question about 2d matrix and pointer in the function definition
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
.

User: "James Daughtry"

Title: Re: question about 2d matrix and pointer in the function definition 11 Jun 2005 09:40:28 AM
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);
.
User: "Rolf Magnus"

Title: Re: question about 2d matrix and pointer in the function definition 11 Jun 2005 03:45:45 PM
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);

.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER