Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointers and arrays

why does the pointer array "equivalence" not work 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

like image 679
user695652 Avatar asked Dec 06 '22 21:12

user695652


1 Answers

The memory model of int** and int[2][2] is different.
int a[2][2] is 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]

while addr1 and addr2 are just addresses in memory.
You just can't convert one to the other.

like image 64
Dani Avatar answered Dec 28 '22 10:12

Dani