A pointer to 2d array

后端 未结 6 1757
予麋鹿
予麋鹿 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:05

    Stricly speaking, no, int (*p)[3] = a; is not a pointer to a. It is a pointer to the first element of a. The first element of a is an array of three ints. p is a pointer to an array of three ints.

    A pointer to the array a would be declared thus:

    int (*q)[2][3] = &a; 
    

    The numeric value of p and q are likely (or maybe even required to be) the same, but they are of different types. This will come into play when you perform arithmetic on p or q. p+1 points to the second element of array a, while q+1 points to the memory just beyond the end of array a.

    Remember: cdecl is your friend: int a[2][3], int (*q)[2][3].

提交回复
热议问题