简体   繁体   English

为什么 cout << *lkop[4] 的 output 是 0?

[英]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;我期待一个 int 4 出现,但 output 是 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] . lkop[4]超出范围,因为lkop指向的arr只有一个int[10]元素。

To do dereferencing first, you should add parenthesis: cout << (*lkop)[4];要首先进行取消引用,您应该添加括号: cout << (*lkop)[4]; . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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