Why is arr and &arr the same?

前端 未结 6 728
鱼传尺愫
鱼传尺愫 2020-12-07 21:12

I have been programming c/c++ for many years, but todays accidental discovery made me somewhat curious... Why does both outputs produce the same result in the code below? (<

相关标签:
6条回答
  • 2020-12-07 21:40
    #include <cassert>
    
    struct foo {
        int x;
        int y;
    };
    
    int main() {    
        foo f;
        void* a = &f.x;
        void* b = &f;
        assert(a == b);
    }
    

    For the same reason the two addresses a and b above are the same. The address of an object is the same as the address of its first member (Their types however, are different).

                                arr
                          _______^_______
                         /               \
                        | [0]   [1]   [2] |
    --------------------+-----+-----+-----+--------------------------
          some memory   |     |     |     |        more memory
    --------------------+-----+-----+-----+--------------------------
                        ^
                        |
               the pointers point here
    

    As you can see in this diagram, the first element of the array is at the same address as the array itself.

    0 讨论(0)
  • 2020-12-07 21:40

    The two have the same value but different types.

    When it's used by itself (not the operand of & or sizeof), arr evaluates to a pointer to int holding the address of the first int in the array. &arr evaluates to a pointer to array of three ints, holding the address of the array. Since the first int in the array has to be at the very beginning of the array, those addresses must be equal.

    The difference between the two becomes apparent if you do some math on the results:

    arr+1 will be equal to arr + sizeof(int).

    ((&arr) + 1) will be equal to arr + sizeof(arr) == arr + sizeof(int) * 3

    Edit: As to how/why this happens, the answer is fairly simple: because the standard says so. In particular, it says (§6.3.2.1/3):

    Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue.

    [note: this particular quote is from the C99 standard, but I believe there's equivalent language in all versions of both the C and C++ standards].

    In the first case (arr by itself), arr is not being used as the operand of sizeof, unary &, etc., so it is converted (not promoted) to the type "pointer to type" (in this case, "pointer to int").

    In the second case (&arr), the name obviously is being used as the operand of the unary & operator -- so that conversion does not take place.

    0 讨论(0)
  • 2020-12-07 21:44

    They're not the same. They just are at the same memory location. For example, you can write arr+2 to get the address of arr[2], but not (&arr)+2 to do the same.

    Also, sizeof arr and sizeof &arr are different.

    0 讨论(0)
  • 2020-12-07 21:49

    Pointers and arrays can often be treated identically, but there are differences. A pointer does have a memory location, so you can take the address of a pointer. But an array has nothing pointing to it, at runtime. So taking the address of an array is, to the compiler, syntactically defined to be the same as the address of the first element. Which makes sense, reading that sentence aloud.

    0 讨论(0)
  • 2020-12-07 21:59

    The address is the same but both expressions are different. They just start at the same memory location. The types of both expressions are different.

    The value of arr is of type int * and the value of &arr is of type int (*)[3].

    & is the address operator and the address of an object is a pointer to that object. The pointer to an object of type int [3] is of type int (*)[3]

    0 讨论(0)
  • 2020-12-07 22:04

    They are not the same.

    A bit more strict explanation:

    arr is an lvalue of type int [3]. An attempt to use arr in some expressions like cout << arr will result in lvalue-to-rvalue conversion which, as there are no rvalues of array type, will convert it to an rvalue of type int * and with the value equal to &arr[0]. This is what you can display.

    &arr is an rvalue of type int (*)[3], pointing to the array object itself. No magic here :-) This pointer points to the same address as &arr[0] because the array object and its first member start in the exact same place in the memory. That's why you have the same result when printing them.


    An easy way to confirm that they are different is comparing *(arr) and *(&arr): the first is an lvalue of type int and the second is an lvalue of type int[3].

    0 讨论(0)
提交回复
热议问题