A pointer to 2d array

后端 未结 6 1770
予麋鹿
予麋鹿 2020-11-28 22:26

I have a question about a pointer to 2d array. If an array is something like

int a[2][3];

then, is this a pointer to array a?<

6条回答
  •  伪装坚强ぢ
    2020-11-28 23:03

    int a[2][3];
    

    a is read as an array 2 of array 3 of int which is simply an array of arrays. When you write,

    int (*p)[3] = a;

    It declares p as a pointer to the first element which is an array. So, p points to the array of 3 ints which is a element of array of arrays.

    Consider this example:

            int a[2][3]
    +----+----+----+----+----+----+
    |    |    |    |    |    |    |
    +----+----+----+----+----+----+
    \_____________/
           |
           |    
           |
           p    int (*p)[3]
    

    Here, p is your pointer which points to the array of 3 ints which is an element of array of arrays.

提交回复
热议问题