First of all, the perfect answer is @mikillskegg .
Secondly, here is a conceptually clear solution for you: if you already write in C ++, use C ++ idioms, they will seem more intuitive to you. In particular, use std :: vector instead of native arrays. Your code will look like this:
#include <iostream> #include <vector> using namespace std; void print(const vector< vector<int> >& v) { for (auto it = v.begin(); it != v.end(); ++it) { for (auto it2 = it->begin(); it2 != it->end(); ++it2) cout << *it2; cout << endl; } } int main() { vector< vector<int> > b(5); for (auto it = b.begin(); it != b.end(); ++it) *it = { 1, 2, 3, 4, 5 }; print(b); return 0; }
Thirdly, we will analyze the errors. Let me explain what went wrong. int**
is a pointer to an array of pointers , right? But int[5][5]
is not a pointer array at all! This is a continuous piece of memory to which the compiler knows how to access the index, because it knows the size of each row in your table. Therefore print(b);
you did not compile, because the types are different, the arrangement of elements in the memory is not compatible! When you convert it to int**
with a hard cast, your pointer to the first element of the table is interpreted as a pointer to a list of pointers, of course, this will not work. Remember: if you need a cast, maybe something goes wrong.
Fourth, if you are still left with native arrays, here's a variation of the @mikillskegg answer closer to the C ++ style (the idea with the template is borrowed from SO):
template <int LINESIZE> void print (int m[][LINESIZE]) { for(int i=0; i<LINESIZE; i++){ for(int j =0; j<LINESIZE; j++){ printf(" %d", m[i][j]); } printf("\n"); } }
Fifth, here is another option that may be better. So that the location of the elements in memory is compatible with double pointer, you could manually row the table:
const int SIZE = 5; void print(int** m) { for(int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) printf(" %d", m[i][j]); printf("\n"); } } // ... int** b = new int*[SIZE]; for(int i = 0; i < SIZE; i++) { b[i] = new int[SIZE]; for (int j = 0; j < size; j++) b[i][j] = j; } print(b);