I want to pass a multi-dimensional array by reference

The code is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

int main()
{
int get_no_eqn, get_no_var;

//get the values for above variables by user

float **matrix = new float* [get_no_eqn]; //creating a multi-dimensional array
for(ptrdiff_t i = 0; i < get_no_eqn; i++)matrix[i] = new float[get_no_var];

interchange(matrix[][], 5) //i think it shouldn't be matrix[][]
return 0;
}

void interchange(float &matrix[][], int final) //probably the wrong step
{
//Get matrix by reference and not by value
}
It's not possible to pass a multidimensional array without specifying all but the last dimension.

Also you probably don't need to pass by reference since matrix is already a pointer to a multidimensional array.

When you pass matrix what you essentially pass is a float** pointer.
You can pass the dimensions as extra arguments though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

int main()
{
int get_no_eqn, get_no_var;

//get the values for above variables by user

float **matrix = new float* [get_no_eqn]; //creating a multi-dimensional array
for(ptrdiff_t i = 0; i < get_no_eqn; i++)matrix[i] = new float[get_no_var];

interchange(matrix, get_no_var, get_no_eqn, 5)
return 0;
}

void interchange(float **matrix, int dim1, int dim2, int final)
{
//matrix is not copied but passed directly (can also be modified)
}
Last edited on
Thanks i m searching this coding
Onur is correct technoupdates!
Onur both is, and is not, correct.

You can use templates to have the compiler generate a function for you. Here's a related thread from not too long ago:
http://www.cplusplus.com/forum/general/49079/

Good luck!
When you pass matrix what you essentially pass is a float** pointer.

It's not really so. You pass float (*)[ first_dimension ] pointer.
It's not really so. You pass float (*)[ first_dimension ] pointer.


I don't think so.

Take a look at the definition of matrix:
float **matrix = new float* [get_no_eqn]; //creating a multi-dimensional array

There is no dimension encoded in the type.
Ok. I didn't notice it. I thought that matrix is a static variable.
Topic archived. No new replies allowed.