c - Runtime error in the following code -


the following code,according me should run successfully,but fails @ runtime.i don't reason:

 void main()  {    int arr[5][3]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};    int *m=arr[0];    int **p=&m;    p=p+1;    printf("%d",**p);    } 

a.exe has stopped working @ runtime in gcc compiler,windows 7 64 bit

an array of arrays , pointer pointer quite different, , can't used interchangeably.

for example, if @ array arr looks in memory

 +-----------+-----------+-----------+-----------+-----+-----------+ | arr[0][0] | arr[0][1] | arr[0][2] | arr[1][0] | ... | arr[4][2] | +-----------+-----------+-----------+-----------+-----+-----------+ 

when have pointer-to-pointer p program don't knows points array of arrays, instead it's treated array of pointers, looks in memory:

 +------+------+------+-----+ | p[0] | p[1] | p[2] | ... | +------+------+------+-----+   |      |      |   |      |      v   |      |        |      v   |        v   

so when p + 1 p[1] not same arr[1].


Comments