Reputation: 29
So, i wrote this code:
int arr[10] = {0,1,2,3,4,5,6,7,8,9};
int (*lkop)[10] = &arr;
cout << *lkop[4];
I was expecting an int 4 to show up, but the output is a 0; Why is that happening, iam really confused.Any ideas? Thank u in advance.
Upvotes: 0
Views: 65
Reputation: 75062
The []
operator has higher precedence than the *
operator.
lkop[4]
is out-of-range because arr
, which lkop
points, has only one element of int[10]
.
To do dereferencing first, you should add parenthesis: cout << (*lkop)[4];
.
Upvotes: 6