What does getting the address of an array variable mean?

前端 未结 7 1619
無奈伤痛
無奈伤痛 2020-12-01 09:16

Today I read a C snippet which really confused me:

#include 

int
main(void)
{
    int a[] = {0, 1, 2, 3};

    printf(\"%d\\n\", *(*(&a +         


        
7条回答
  •  死守一世寂寞
    2020-12-01 09:48

    Best to prove it to yourself:

    $ cat main.c
    #include 
    main()
    {
      int a[4];
      printf("a    %p\n",a);
      printf("&a   %p\n",&a);
      printf("a+1  %p\n",a+1);
      printf("&a+1 %p\n",&a+1);
    }
    

    And here are the addresses:

    $ ./main
    a    0x7fff81a44600 
    &a   0x7fff81a44600 
    a+1  0x7fff81a44604
    &a+1 0x7fff81a44610
    

    The first 2 are the same address. The 3rd is 4 more (which is sizeof(int)). The 4th is 0x10 = 16 more (which is sizeof(a))

提交回复
热议问题