Pointers and Arrays

why does the equivalence matrix of pointers fail in the following case?

void foo(int** x) { cout << x[0][1]; } int main( ) { int a[2][2] = {{1,2},{2,3}}; foo(a); } 

Thank you

+4
source share
3 answers

The memory model int ** and int [2] [2] is different.
int a[2][2] stored in memory as:

 &a : a[0][0] &a + 4 : a[0][1] &a + 8 : a[1][0] &a + 12: a[1][1] 

int** x :

 &x : addr1 &x + 4 : addr2 addr1 : x[0][0] addr1 + 4: x[0][1] addr2 : x[1][0] addr2 + 4: x[1][1] 

and addr1 and addr2 are only addresses in memory.
You simply cannot convert one into another.

+11
source

This does not work, because only the first level of the multidimensional array splits into a pointer. Try the following:

 #include <iostream> using std::cout; void foo(int (*x)[2]) { cout << x[0][1]; } int main( ) { int a[2][2] = {{1,2},{2,3}}; foo(a); } 
+4
source

because the type is not int **. that's right for the foo function

foo (int * [2]);

Pointer type a is not int **, exactly int * [2] ..

0
source

All Articles