Passing a two-dimensional array to a function like a pointer argument.
I don''t know how to pass a a two-dimensional array in a function POINTER argument
here is my code :
#include
using namespace std;
double yield(double *beans, int count);
int main(void){
double beansA[3][4] = { { 1.0, 2.0, 3.0, 4.0 },
{ 5.0, 6.0, 7.0, 8.0 },
{ 9.0, 10.0, 11.0, 12.0 } };
double beansB[2][3] = { { 1.0, 2.0, 3.0 },
{ 5.0, 6.0, 7.0 } };
double sumA=0.0, sumB=0.0;
cout << "Display beans A = " <<
yield(beansA, sizeof beansA/sizeof beansA[0]);
cout << "sum beans A = " << sumA ;
cout << "Display beans B = " <<
yield(beansB, sizeof beansB/sizeof beansB[0]);
cout << "sum beans B = " << sumB ;
cout << endl; return 0;
}
// Function to compute to display a tab and a total yield
double yield(double *beans, int count)
{
double sum = 0.0;
for(int i=0; i<count; i++) // Loop through number of rows
{
for(int j=0; j<4; j++) // Loop through elements in a row
{
cout << *(*(beans+i)+j) << " " ;
sum += *(*(beans+i)+j);
}
cout << endl;
}
return sum;
}
VC++
generate error :
error C2664: ''yield'' : cannot convert parameter 1 from ''double [3][4]'' to ''double *''
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
How must can i do ? Help me please.
Thanks to explain my error ?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement