I wrote the following code to point to first row of a 2-dimensional array. However, when I do
arrayPtr = & array[0];
I end up getting
error: cannot convert
double (*)[1]
todouble*
in assignmentarrayPtr = & array[0];
My program is:
#include <iostream>
int main(int argc, char **argv)
{
double array[2][1];
array[0][1] = 1.0;
array[1][1] = 2.0;
double* arrayPtr;
arrayPtr = &array[0];
return 0;
}
Can someone help me understand as to where am I going wrong?
Instead of
arrayPtr = & array[0]
, you can writeto make use of array decay property.
Related,
Quoting
C11
, chapter §6.3.2.1, Lvalues, arrays, and function designatorsQuoting
C++14
, chapter §5.3.3and, for chapter 4.2,
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 typedouble*
which is the same as the LHS.Otherwise, the use of
&
operator prevents the array decay forarray[0]
which is an array of typedouble [1]
.Thus,
&array[0]
returns a type which is a pointer to an array ofdouble [1]
, or,double (*) [1]
which is not compatible with the type of the variable supplied in LHS of the assignment, adouble *
.