How to specify the base address of 2d array pointers

Let's pretend that

int a[2][3] ;
int (*p)[3]=a; // is ok
int (*p)[3]=&a[0]; // is also ok

but why

int (*p)[3]=a[0]; 

it creates errors, although a[0]it gives the first address of the array (since 2d arrays are an array of arrays) and it seems more normal than which &a[0], which gives the address of the first element of the first array, is still ok, but why?

+3
source share
4 answers

, sizeof & , , "N-element array of T" ( "" ) " T", .

int a[2][3];

:

        Expression        Type            Decays To      Equivalent Value
        ----------        ----            ---------      ----------------
                 a        int [2][3]      int (*)[3]     &a[0]
                &a        int (*)[2][3]   n/a            n/a
                *a        int [3]         int *          a[0]
              a[i]        int [3]         int *          n/a
             &a[i]        int (*)[3]      n/a            n/a
             *a[i]        int             n/a            a[i][0]
           a[i][j]        int             n/a            n/a

, a, &a, *a, a[0], &a[0] &a[0][0] ( ), .

, a[0] "3- int"; sizeof &, " int", " 3- int", int (*p)[3] = a[0]; .

+5

a[0] , int[3]. 3 , , .

+2
int (*p)[3]=a; // is OK  

a int (*)[3] ( 3 int s), , p. .

int (*p)[3]=&a[0]; // is also OK  

&a[0] int (*)[3] ( )

int (*p)[3]=a[0]; // is not OK  

a[0] int * 0. .

+2
source

How [0] can be of type int or int [3], since we declared it a 2d array

0
source

All Articles