Instead of arrayPtr = & array[0]
, you can write
arrayPtr = array[0];
to make use of array decay property.
Related,
Quoting C11
, chapter §6.3.2.1, Lvalues, arrays, and function designators
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. [...]
Quoting C++14
, chapter §5.3.3
The lvalue-to-rvalue (4.1)
, array-to-pointer (4.2)
, and function-to-pointer (4.3)
standard conversions are not
applied to the operand of sizeof
.
and, for chapter 4.2,
An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue
of type “pointer to T”. The result is a pointer to the first element of the array.
So, while used as the RHS operand of the assignment operator, array[0]
decays to the pointer to the first element of the array, i.e, produces a type double*
which is the same as the LHS.
Otherwise, the use of &
operator prevents the array decay for array[0]
which is an array of type double [1]
.
Thus, &array[0]
returns a type which is a pointer to an array of double [1]
, or, double (*) [1]
which is not compatible with the type of the variable supplied in LHS of the assignment, a double *
.