指针的指针澄清 [英] Pointer to pointer clarification
问题描述
我是按照此教程了解如何做一个的指针的指针的工作。
让我引述相关段落:
INT I = 5,J = 6,K = 7;
为int * IP1 =放;我,* IP2 =安培;焦耳;现在我们可以通过设置
INT ** IPP =安培; IP1;
和
IPP
点IP1
指向I
。* IPP
是IP1
和** IPP
是I
,或5我们可以说明情况,与我们熟悉的盒子和箭头符号,像这样的:
如果那么我们说
* IPP = IP2;
我们已经改变了指针指向
IPP
(即IP1
)包含的副本IP2
,所以它(IP1
)现在指向在Ĵ
:
块引用>我的问题是:为什么在第二张图片,是
IPP
仍指向IP1
而不是IP2
?解决方案忘记了第二个有关定点类比。什么是真正的指针包含一个内存地址。在
&安培;
是运营商的地址 - 即它在对象的内存返回的地址。在*
运营商给你一个指针指向给定的包含地址的指针,它返回的内存地址中的对象,即对象。所以,当你做* IPP = IP2
,你在做什么是* IPP
获取对象在保存的地址IPP
是IP1
,然后分配到IP1
价值存储在IP2
,这是Ĵ
的地址。简单结果
&安培;
- >结果的地址*
- >在I was following this tutorial about how does a pointer to a pointer work.
Let me quote the relevant passage:
int i = 5, j = 6, k = 7; int *ip1 = &i, *ip2 = &j;
Now we can set
int **ipp = &ip1;
and
ipp
points toip1
which points toi
.*ipp
isip1
, and**ipp
isi
, or 5. We can illustrate the situation, with our familiar box-and-arrow notation, like this:If then we say
*ipp = ip2;
we've changed the pointer pointed to by
ipp
(that is,ip1
) to contain a copy ofip2
, so that it (ip1
) now points atj
:
My question is: Why in the second picture, is
ipp
still pointing toip1
but notip2
?解决方案Forget for a second about the pointing analogy. What a pointer really contains is a memory address. The
&
is the "address of" operator - i.e. it returns the address in memory of an object. The*
operator gives you the object a pointer refers to, i.e. given a pointer containing an address, it returns the object at that memory address. So when you do*ipp = ip2
, what you are doing is*ipp
get the object at the address held inipp
which isip1
and then assign toip1
the value stored inip2
, which is the address ofj
.Simply
&
--> Address of
*
--> Value at这篇关于指针的指针澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!