Reputation: 3041
int b[3][2] = { {0, 1}, {2, 3}, {4, 5} };
int (*bpp)[2] = b;
int *bp = b[0];
At the above code: Is *bpp
a pointer to a two-dimensional array? Or an array of pointers with the length of 2? Why is *bpp
surrounded with parenthesis? Is there a difference between *bpp[2]
and (*bpp)[2]
?
Meantime, in the following code: (Changing the dimension of the array)
int i[4] = { 1, 2, 3, 4 };
int (*ap)[2] = (int(*)[2])i;
The second line is very confusing to me, especially the typecasting (int(*)[2])
, what data type is it exactly casting to?
Thank you ^^
Upvotes: 2
Views: 179
Reputation: 40633
bpp
is a pointer to an array of two int
. *bpp
is an array of two int
. int *bpp[2]
would declare bpp
as an array of two pointers to int
(this parentheses make it be a pointer to an array of two int
).
(int(*)[2])
is a cast to a pointer to an array of two int
.
These can be read by considering the "declaration follows use" rule (combined with knowledge of operator precedence):
dereference (so bpp is a pointer)
|
v
int (*bpp)[2]
^ ^
| |
| array index (so the thing that bpp points to is an array)
|
the thing on the left is the final type... here it is int,
so the array is an array of int
Upvotes: 3