Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing three dimensional array by reference in c++

Function is defined as:

int strip(double *signalstnV){
.
.
return 0;
}

And main function it is called as:

main{
.
.
double signalstnV[iteration][2*range+1][3];
.
.
check = strip(signalstnV);
.
}

I wanted to use the array in next function in main after it is modified in strip function. But during compilation i am getting an error as following

sim.C: In function ‘int main(int, char**)’:

sim.C:54:26: error: cannot convert ‘double ()[151][3]’ to ‘double’ for argument ‘1’ to ‘int strip(double*)’

check = strip(signalstnV);

I am not able to understand it. Please help. My main goal is to generate array from strip function and pass it to other functions later in code.

Also when i used this array in another function

threshold(double * signalstnV)

and using a for loop to extract some specific values, it gives error as:

invalid types ‘double[int]’ for array subscript 
if (signalstnV[k][j][3] < -0.015){
..}
like image 244
Purnendu Kumar Avatar asked Mar 11 '23 20:03

Purnendu Kumar


2 Answers

An ordinary single-dimensional array decays to a pointer to its first value. A multi-dimensional array decays only in its first dimension, decaying to a pointer to the initial 2nd dimension of the array.

If you wish to "super-decay" a multi-dimensional array to a pointer to its first value, then simply do that yourself:

check = strip(&signalstnV[0][0][0]);
like image 132
Sam Varshavchik Avatar answered Mar 16 '23 07:03

Sam Varshavchik


For such cases, with more or less complicated types - use type aliases:

//double signalstnV[iteration][2*range+1][3];
using IterationValues = double[iteration];
using IterationRangesValues = IterationValues [2*range+1];
//...

then, it is obvious that calling this:

IterationRangesValues signalstnV[3];
check = strip(signalstnV);

You need this signature: strip(IterationRangesValues * matrix) or this strip(IterationRangesValues (& matrix)[3]). Of course for this last array you might use typedef too.

You might also use std::array:

using IterationValues = std::array<double, iteration>;
using IterationRangesValues = std::array<IterationValues, 2*range+1>;
//...
like image 35
PiotrNycz Avatar answered Mar 16 '23 08:03

PiotrNycz