Are a, &a, *a, a[0], &a[0] and &a[0][0] identical pointers?

后端 未结 8 1632
南旧
南旧 2020-11-27 17:45

I have the following C program:

#include 

int main(){
    int a[2][2] = {1, 2, 3, 4};
    printf(\"a:%p, &a:%p, *a:%p \\n\", a, &a, *         


        
8条回答
  •  半阙折子戏
    2020-11-27 18:14

    You know that a is the address of the first element of your array and according to the C standard, a[X] is equal to *(a + X).

    So:

    &a[0] == a because &a[0] is the same as &(*(a + 0)) = &(*a) = a.

    &a[0][0] == a because &a[0][0] is the same as &(*(*(a + 0) + 0))) = &(*a) = a

提交回复
热议问题