How does de-referencing work for pointer to an array?

前端 未结 5 1403
陌清茗
陌清茗 2021-01-24 09:53

Pointer to array of elements when dereferenced return an address. Since it is holding the address of the first element of the array, dereferencing it should return a value.

5条回答
  •  孤街浪徒
    2021-01-24 10:45

    Why does *ptr return the base address of the array, shouldn't it return the value at that address??

    (p3) Except when it is the operand of the sizeof operator, the _Alignof 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. C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3)

    int (*ptr)[5] = &arr;
    

    ptr is a pointer-to-array of int [5]. When you dereference ptr you get array of int[5]. How is an array of int[5] accessed?

    Rule 6.3.2.1 provides the answer:

    "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object...

    Now if you dereference again (e.g. **ptr), then you get the value of the 1st element.

提交回复
热议问题