Reputation: 1963
In int (*x)[10];
x
is a pointer to an array of 10 int
s
So why does this code not compile:
int arr[3] ;
int (*p)[3] =arr;
But this works:
int arr[3];
int (*p)[3] =&arr;
Upvotes: 5
Views: 117
Reputation: 340178
arr
is an expression that evaluates to an int*
(this is the famous 'arrays decay to pointer' feature).
&arr
is an expression that evaluates to a int (*)[3]
.
Array names 'decay' to pointers to the first element of the array in all expressions except when they are operands to the sizeof
or &
operators. For those two operations, array names retain their 'arrayness' (C99 6.3.2.1/3 "Lvalues, arrays, and function designators").
Upvotes: 10
Reputation: 136208
It doesn't work for exactly the same reason as:
int i;
int* pi = i; // error: no conversion from int to int*
Upvotes: 0