🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Passing a two-dimensional array to a function like a pointer argument.

Started by
0 comments, last by patmaba2 22 years, 10 months ago
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 ?
Advertisement
Try a pointer to a pointer eg double**

This topic is closed to new replies.

Advertisement