A 2-dimensional array is not the same as an array of pointers, which is interpreted as int**. Change the return type of gettab.
int* gettab(int tab[][2]){
return &tab[0][0];
}
int main() {
int a[4][2] = {{0, 0}, {1, 0}, {2, 0}, {2, 1}};
int* b = gettab(a);
cout << b[0]; // b[row_index * num_cols + col_index]
cout << b[1 * 2 + 0]; // the 1 from {1, 0}
}
Or:
int (*gettab(int tab[][2]))[2] {
return tab;
}
template<class T> struct identity { typedef T type; };
identity<int(*)[2]>::type gettab(int tab[][2]) {
return tab;
}
int main() {
int a[4][2] = {{0, 0}, {1, 0}, {2, 0}, {2, 1}};
int (*b)[2] = gettab(a);
cout << b[0][0];
}
Roger Pate
source
share