Hasakiss Haselioss
Hasakiss Haselioss

Reputation: 29

Why does the output of cout << *lkop[4] is a 0?

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

Answers (1)

MikeCAT
MikeCAT

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

Related Questions