C ++ - * p vs& p vs p [英] C++ - *p vs &p vs p
问题描述
我仍然很难理解* p,& p和p之间的区别。从我的理解,*可以被认为是价值指向,&作为地址。换句话说,*保存值,而&持有地址。如果这是真的,那么* p和p之间的区别是什么? p <$ p
I am still struggling to understand the difference between *p, &p, and p. From my understanding, * can be thought of "value pointed by", and & as "adress of". In other words, * holds the value while & holds the adress. If this is true, then what is the distinction between *p and p? Doesn't p hold the value of something, just like *p?
推荐答案
*
运算符用于间接。间接表示 p
中的值被解释为内存地址,并加载该地址处的值。 p
是 p
的值,而 * p
值存储在 p
指向的存储器位置。当你想间接访问整数 i
的值时,你可以有一个指向它的整数指针( int * p =& i
)并使用该指针间接修改 i
的值( * p = 10
) 。
The *
operator is used for indirection. Indirection means the value in p
is interpreted as a memory address and the value at that address is loaded. p
is the value of p
while *p
is the value stored in the memory location pointed by p
. When you want to indirectly access the value of an integer i
, you can have an integer pointer point to it (int *p = &i
) and use that pointer to modify the value of i
indirectly (*p = 10
).
这篇关于C ++ - * p vs& p vs p的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!