[英]Query based on Pointer
#include<iostream.h>
void main()
{
int arr[2][3][2]={{{2,4},{7,8},{3,4},}, {{2,2},{2,3},{3,4}, }};
cout<<**(*arr+1)+2+7;
}
According to me answer will be 11, But compiler is showing 16. Can anyone please explain the solution? 根据我的回答,将是11,但是编译器显示16。有人可以解释该解决方案吗? Thanks in advance
提前致谢
*arr
is equivalent to arr[0]
. *arr
等效于arr[0]
。
*(arr[0]+1)
is equivalent to arr[0][1]
. *(arr[0]+1)
等效于arr[0][1]
。
*arr[0][1]
is equivalent to arr[0][1][0]
. *arr[0][1]
等同于arr[0][1][0]
。
So, your code is equivalent to this: 因此,您的代码与此等效:
#include<iostream.h>
void main()
{
int arr[2][3][2]={
{{2,4},{7,8},{3,4},},
{{2,2},{2,3},{3,4},}
};
cout << arr[0][1][0]+2+7;
}
arr[0][1][0]
is 7, so you get 7+2+7, which is 16. arr[0][1][0]
为7,所以得到7 + 2 + 7,即16。
**(*arr + 1) + 2 + 7
Is the same as 是相同的
**(arr[0] + 1) + 2 + 7
Same as 如同
arr[0][1][0] + 2 + 7
arr[0][1][0]
Is 7 by definition. arr[0][1][0]
根据定义为7。
So the compiler is correct and answer is 16. 因此,编译器是正确的,答案为16。
The other answers are correct. 其他答案是正确的。 Try doing this in your code to see it in action for yourself:
尝试在您的代码中执行此操作,以亲自查看实际效果:
#include <iostream>
int main()
{
int arr[2][3][2]=
{
{
{2,4},{7,8},{3,4},
},
{
{2,2},{2,3},{3,4},
}
};
std::cout << *arr << std::endl; // 0x7fff5a3a6710
std::cout << *arr+1 << std::endl; // 0x7fff5a3a6718
std::cout << *(*arr+1) << std::endl; // 0x7fff5a3a6718
std::cout << **(*arr+1) << std::endl; // 7
std::cout << **(*arr+1)+2+7 << std::endl; // 16
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.