如果我有:
int j = 8;
int *pointer = &j;
那么如果我这样做:
&*pointer == *&pointer
返回
1
( true
)。但我对第二个表达有疑问:
&*pointer
返回指针指向的地址(首先求值 *然后 &)
*&pointer
返回指针地址,然后它指向什么......但这是变量而不是地址。所以这是我的疑问... 最佳答案
pointer
“指向”内存中的某个地址;它驻留在内存中的某个其他地址。
&*pointer // (*pointer) - dereference `pointer`, now you have `j`
// &(*pointer) - the address of `j`(that's the data that `pointer` has)
然而:
*&pointer //(&pointer) - the address of pointer(where pointer resides in memory)
// *(&pointer) - deference that address and you get `pointer`
我总是发现用图片更容易追踪指针,所以也许这个插图将有助于理解为什么它们是相同的:
//In case of &*pointer, we start with the pointer, the dereference it giving us j
//Then taking the address of that brings us back to pointer:
+--&(*pointer)-----------+
| |
memory address 0x7FFF3210 | 0x7FFF0123 |
+------------+ | +-----+ |
data present | pointer = | <---+ +-> | j=8 |----+
| 0x7FFF0123 | ->(*pointer)-+ +-----+
+------------+
//in the *&pointer case, we start with the pointer, take the address of it, then
//dereference that address bring it back to pointer
memory address +------------> 0x7FFF3210 ----*(&pointer)--+
| |
| +------------+ |
data present | | pointer = | <----------- -+
+--&pointer ---| 0x7FFF0123 |
+------------+
关于c++ - 如何阅读这些表达 : *&pointer VS &*pointer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15882272/