c - 3d array print address after deference twice? -


why 3d array print address after being dereferenced twice? please me understand code posted below, (assume array begins @ location 1002).

int main() {    int a[2][3][4]={                    {                      1,2,3,4,                      4,5,6,7,                      9,1,1,2                    },                    {                      2,1,4,7,                      6,7,8,9,                      0,0,0,0                    }                  };     printf("%u %u %u %u\n",a,*a,**a,***a); //a == *a == **a, print address 1002. why?  

}

**a has type int * , points first int in 3d array
*a has type int (*)[4] , points first row of 3d array
a has type int (*)[3][4] , points first 2d array in 3d array
&a has type int (*)[2][3][4] , points whole 3d array

so pointers point same address. it's type of pointer different. following code may illustrate point.

int main( void ) {     int a[2][3][4]={ 1,2,3,4, 4,5,6,7, 9,1,1,2,    2,1,4,7, 6,7,8,9, 0,0,0,0 };      int *ptrint;                // pointer int     int (*ptrarray1)[4];        // pointer array of ints     int (*ptrarray2)[3][4];     // pointer 2d array of ints     int (*ptrarray3)[2][3][4];  // pointer 3d array of ints      ptrint    = **a;     ptrarray1 = *a;     ptrarray2 = a;     ptrarray3 = &a;      printf( "%p %p\n", ptrint   , ptrint    + 1 );     printf( "%p %p\n", ptrarray1, ptrarray1 + 1 );     printf( "%p %p\n", ptrarray2, ptrarray2 + 1 );     printf( "%p %p\n", ptrarray3, ptrarray3 + 1 ); } 

note: left out inner braces in array initialization demonstrate inner braces optional. best practice have of inner braces.

typical output code shown below. i've added comments show difference between 2 pointers decimal number.

0x17b00 0x17b04  //  4 bytes, hence pointer int 0x17b00 0x17b10  // 16 bytes, pointer int[4] 0x17b00 0x17b30  // 48 bytes, pointer int[3][4] 0x17b00 0x17b60  // 96 bytes, pointer int[2][3][4] 

note when add 1 pointer, size of object added pointer. example, if have int * , add 1 pointer, value of pointer increase 4 because sizeof(int) == 4. (yes, assumes ints 32-bits, thank you.)

so adding 1 pointer, can determine size of object pointer points to. gives clue type of pointer compiler's point of view. in example above, notice adding 1 ptrarray1 changes pointer 16. that's because ptrarray1 points object of size 16, points array of 4 ints.


just we're confused, allow me following line of code print number 8. chose 8 since appears once in array, can tell it's coming from.

    printf( "%d\n", ptrarray3[0][1][1][2]); 

notice appears i'm using ptrarray3 4-dimensional array. why pointers multidimensional arrays confusing in c. when convert array pointer, pointer has 1 less dimension array. when use pointer array syntax, use though had 1 more dimension.

so example, start 2d array

int array[4][100]; 

the corresponding pointer pointer 1d array

int (*ptr)[100] = array; 

but can use pointer 2d array

ptr[2][100] = 6; 

that basis of confusion, , reason pointer-to-array seldom used feature in c.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -