# ? passing multi dimensional array to function

• 02-09-2002
rc7j
? passing multi dimensional array to function
The function call: " printAverage(dArray);" produces an error. The Visual c++ error message is: "cannot convert parameter 1 from 'double [3][2][5][2]' to 'double'"

'dArray' in its decayed form is a 'pointer to a double'. Where is the error??

#include <iostream>
#include <cstdlib>
using namespace std;

const int dDIM1 = 3, dDIM2 = 2, dDIM3 = 5, dDIM4 = 2;
const int SEED = 100;

double (*printAverage(double dArray))[dDIM2][dDIM3][dDIM4];

#define elements(array) (sizeof(array)/sizeof(double))

int main()
{
double dArray[dDIM1][dDIM2][dDIM3][dDIM4], *elPtr;

srand(SEED);
for(elPtr = (double *)dArray; elPtr < (double *)(&dArray+1); ++elPtr)
*elPtr = (double)rand(); // populate the elements of the array

printAverage(dArray); // call function

return (0);
}

double printAverage(double dArray[][dDIM2][dDIM3][dDIM4])
{
int i, j, k, m;
double total = 0;

for(i = 0; i < dDIM1; ++i)
for(j = 0; j < dDIM2; ++j)
for(k = 0; k < dDIM3; ++k)
for(m = 0; m < dDIM4; ++m)
total += dArray[i][j][k][m]; // get total value of elements

cout << "The value of the array is " << total << endl;
cout << "The average of the array is " << total/elements(dArray);

return(0);
}
• 02-10-2002
Salem
Perhaps because your prototype is mangled