简体   繁体   中英

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.

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]; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM