我很难理解这两者之间的区别:(int *) (&a[2]) 和 *(&a[2])。我理解 &a[2] 获取 a[2] 的地址并 * 获取该地址中的值,因此 *(&a[2]) 获取 a[2] 的值,即 3。但是 (int * )(&a[2]) 返回一个地址?谢谢!
输出:
3
0x7fff40144888
代码:
include<iostream>
using namespace std;
int main()
{
int a[3]={1,2,3};
//cout << *(a+2) << endl;
cout << *(&a[2]) << endl;
cout << (int*)(&a[2]) << endl;
}
最佳答案
*(&a[2])
获取值 a[2]
(一个简单的引用然后取消引用)
(int*)(&a[2])
获取 a[2]
的地址并将其转换为 int*(无解引用)
(int*)
不引用指针,它将它转换为 int*
(这是多余的,因为它已经是一个 int*
)
注意 int*
是一种类型,就像int
是一种类型一样。
关于c++ - (int *) (&a[2]) 和 *(&a[2]) 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31443573/