Home  >  Q&A  >  body text

c++ - 数组名取地址

 int a[2];
 printf("%x  %x\n", a, &a);  // a &a 输出一样

怎么解释这种现象

伊谢尔伦伊谢尔伦2713 days ago523

reply all(9)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 14:00:17

    Array name, when used to access array elements, it represents the first address of the array, and when you use it to access the array itself, it is the address of the array. It's just that the first address of the array is the same as the address of the array.

    a means &a[0], a+1 means &a[1]... At this level, the array name is equivalent to a pointer that can access the internal elements of the array.

    &a is the address of the "object" of the array, and &a + 1 represents another array with the same size as the array a. At this level, &a is equivalent to a pointer to access the array as a whole - a pointer to the array.

    reply
    0
  • 阿神

    阿神2017-04-17 14:00:17

    C-style arrays are flat on the stack and have no other members except the array elements. So, isn’t it normal that the first element of the array is the same as the array address? Or what to call the "first" element.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 14:00:17

    There is a method that may help you understand
    Execute the following code

    int a[64];
    printf("%x  %x  %x  %x", a, a+1, &a, (&a)+1);

    On my machine, the running results are:
    29fe10 29fe14 29fe10 29ff10
    The difference between the first result and the second result is 4, and the difference between the third result and the fourth result is 0x100
    4 It is exactly the length of int, 0x100 (256) is exactly the length of an int array with a length of 64

    a is the address of the first element of this array, so you can access the i-th element using *(a+i), and &a is the address of this array, but because the address of a piece of data is its first word section address, so their values ​​are the same

    reply
    0
  • 迷茫

    迷茫2017-04-17 14:00:17

        char A[20][20][20][20];
        printf("%x  %x  %x  %x  %x ", A[0][0][0] ,A[0][0],A[0], A, &A);

    These five values ​​are exactly the same

    reply
    0
  • 阿神

    阿神2017-04-17 14:00:17

    If a variable stores its own address, then taking the address 10,000 times is also its own address,

    The above is incorrect.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 14:00:17

    In C language, when the array name is used as a function parameter, it will be converted into a pointer pointing to the first element of the array , and because the address of the first element of the array is equal to the array address , so the a and &a parameters passed to printf here, the values ​​ corresponding to their objects inside the function are equal, and both are equal to the address of the array.

    reply
    0
  • 阿神

    阿神2017-04-17 14:00:17

    • &a is the address of the array

    • a is the first address of the first element of the array

    • &a + 1 has gone outside the array

    • a + 1 is the address of the second element of the array

    reply
    0
  • 阿神

    阿神2017-04-17 14:00:17

    The values ​​​​of a and &a are both addresses, and the two addresses are equal.

    But the types of a and &a are different. Specifically:
    The type of a is int *
    The type of &a is int (*)[2]

    This type will affect the results of pointer addition and subtraction operations, so the results of a+1 and (&a)+1 are different.
    In this example, a+1 is 4 bytes more than a, and (&a)+1 is 8 bytes more than &a

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 14:00:17

    In C, the value of the array name is a pointer constant, which is the address of the first element of the array
    So
    &a, a, &a[0] are equivalent

    reply
    0
  • Cancelreply