Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to pass a matrix to a function

Reply
Thread Tools

How to pass a matrix to a function

 
 
Wei-Chao Hsu
Guest
Posts: n/a
 
      06-07-2004
There is an error in my program. The error message is

e:\test\test\main.cpp(9): error C2664: 'fun' : cannot convert parameter 1
from 'int [2][3]' to 'int ** '

If I change the argument "int** a" to "int a[2][3], it will be OK. But, I
don't want to change the definition.
How can I pass the matrix to the function?

//-------------------------------------------------------------
void fun(int **a,const int row,const int col);

void main()
{

int a[2][3];
fun(a,2,3);
}

void fun(int **a,const int row,const int col)
{
for(int i=0;i<row;++i)
for(int j=0;j<col;++j)
a[i][j]=i*10+j;
}


 
Reply With Quote
 
 
 
 
Frane Roje
Guest
Posts: n/a
 
      06-07-2004
Try making your int **a into an int (*a)[3] this way you actualy tell the
computer you want a pointer to an array that has a 3 ints in it. If you had
an **int this is just double indirection, and with(*)[] you have a pointer
to an array.

HTH

--
Frane Roje

Have a nice day

Remove (d*el*ete) from email to reply


 
Reply With Quote
 
 
 
 
Frane Roje
Guest
Posts: n/a
 
      06-07-2004
Try making your int **a into an int (*a)[3] this way you actualy tell the
computer you want a pointer to an array that has a 3 ints in it. If you had
an **int this is just double indirection, and with(*)[] you have a pointer
to an array.

HTH

--
Frane Roje

Have a nice day

Remove (d*el*ete) from email to reply



 
Reply With Quote
 
Niels Dybdahl
Guest
Posts: n/a
 
      06-07-2004
> e:\test\test\main.cpp(9): error C2664: 'fun' : cannot convert parameter 1
> from 'int [2][3]' to 'int ** '
>
> If I change the argument "int** a" to "int a[2][3], it will be OK. But, I
> don't want to change the definition.
> How can I pass the matrix to the function?


Usually int [2][3] is implemented as a flat array while int** is a pointer
to a pointer which is something completely different.
So it is not possible to transfer int a[2][3] as int**

Niels Dybdahl


 
Reply With Quote
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      06-07-2004
In article <ca1f5m$292u$>, Wei-Chao Hsu wrote:
> There is an error in my program. The error message is
>
> e:\test\test\main.cpp(9): error C2664: 'fun' : cannot convert parameter 1
> from 'int [2][3]' to 'int ** '


That can't be done. int a[2][3] is not stored as an a**. It's stored
as an array, whith each element as an int[3]. You could define fun() like
this:

void fun(int a[][3], const int row)
{
for (...) for (...) a[i][j]=...;
}

Note all but the first index needs to be known at compile time. You
could also define fun() like this:

void fun(int *a, const int row, const int col)
{
for (int i=0;i<row;++i) for (int j=0;j<col;++j)
a[i*col + j] = i*10+j;
}

but then fun must be called as this:

fun(reinterpret_cast<int*>(a), 2, 3)

There is more than one way to do things, but: C++ has something called
classes. To implement a matrix is better done using this feature than
plain arrays.

> If I change the argument "int** a" to "int a[2][3], it will be OK. But, I
> don't want to change the definition.
> How can I pass the matrix to the function?


You would need to change the definition of fun.

> //-------------------------------------------------------------
> void fun(int **a,const int row,const int col);
>
> void main()


int main()

> {
>
> int a[2][3];
> fun(a,2,3);
> }
>
> void fun(int **a,const int row,const int col)
> {
> for(int i=0;i<row;++i)
> for(int j=0;j<col;++j)
> a[i][j]=i*10+j;
> }



--
Robert Bauck Hamar
 
Reply With Quote
 
Wei-Chao Hsu
Guest
Posts: n/a
 
      06-07-2004
I just learn C++ recently and need to rewrite some programs from FORTRAN to
C++. In the programs, the sizes of matrices are always determined at
runtime. Passing a multiple dimensional array to a function is very easy to
FORTRAN, but it seems to be a problem to C/C++.
> There is an error in my program. The error message is
>
> e:\test\test\main.cpp(9): error C2664: 'fun' : cannot convert parameter 1
> from 'int [2][3]' to 'int ** '
>
> If I change the argument "int** a" to "int a[2][3], it will be OK. But, I
> don't want to change the definition.
> How can I pass the matrix to the function?
>
> //-------------------------------------------------------------
> void fun(int **a,const int row,const int col);
>
> void main()
> {
>
> int a[2][3];
> fun(a,2,3);
> }
>
> void fun(int **a,const int row,const int col)
> {
> for(int i=0;i<row;++i)
> for(int j=0;j<col;++j)
> a[i][j]=i*10+j;
> }
>
>



 
Reply With Quote
 
Wei-Chao Hsu
Guest
Posts: n/a
 
      06-07-2004
I think I find the solution, but I don't know whether the method for
deallocating is correct. Any idea?
Another question- If I don't delete the memory, will it deallocate the
memory automatically after the function is ended?

//--------------------------------------------------------
void fun(int *a,const int row,const int col);

void main()
{
const row=9;
const col=8;
int a[row][col];
int *pa=&a[0][0];

fun(pa,row,col);
}

void fun(int *pa,const int row,const int col)
{
int **a=new int*[row];
a[0]=pa;
for(int i=1;i<row;++i)
a[i]=&a[i-1][col];

for(int i=0;i<row;++i)
for(int j=0;j<col;++j)
a[i][j]=i*10+j;

delete [] a;
}




 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      06-07-2004
Wei-Chao Hsu wrote:
>
> I just learn C++ recently and need to rewrite some programs from FORTRAN to
> C++. In the programs, the sizes of matrices are always determined at
> runtime. Passing a multiple dimensional array to a function is very easy to
> FORTRAN, but it seems to be a problem to C/C++.


Arrays in general are a pain in the ass in C++. Especially if they
need to be dynamically sized. A std::vector is much easier to
deal with since it is a first class object. An example
of using a std::vector has already been shown I think.

--
Karl Heinz Buchegger

 
Reply With Quote
 
beliavsky@aol.com
Guest
Posts: n/a
 
      06-07-2004
<SNIP>

> void main()
> {
>
> int a[2][3];
> fun(a,2,3);
> }


This does not answer your question, but 'main' should be declared 'int', not 'void'.
 
Reply With Quote
 
Xiaobin Yang
Guest
Posts: n/a
 
      06-07-2004
following code works.

> void main()
> {
>
> int a[2][3];

use "int** a = NULL;"
> fun(a,2,3);
> }
>


void fun(int** & p_Array, const int row,const int col)
{
if (p_Array) delete p_Array;
p_Array = new int* [row];
for (int i = 0; i < row; i++)
p_Array[i] = new int[col];

for(int i=0;i<row;++i)
{for(int j=0;j<col;++j)
{ p_Array[i][j]=i*10+j; }
}
}




-- xiaobin
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Pass multidimensional array (matrix) to c function using ctypes Daniel Platz Python 2 01-07-2010 01:23 PM
How to pass a huge matrix to a function Bernhard Reinhardt C++ 10 09-20-2007 02:26 PM
Matrix composed by two matrix lvcargnini VHDL 3 07-05-2006 07:21 AM
Re: Matrix DTS and Matrix 2 DTS? PeterTHX DVD Video 0 08-03-2003 05:46 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57