指向指针的说明 [英] Pointer to pointer clarification

查看:20
本文介绍了指向指针的说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注这个教程,关于指向一个指针的指针工作.

让我引用相关段落:

<小时><块引用>

 int i = 5, j = 6, k = 7;int *ip1 = &i, *ip2 = &j;

现在我们可以设置

 int **ipp = &ip1;

ipp指向ip1,后者指向i.*ippip1**ippi,或者 5.我们可以举例说明情况,用我们熟悉的方框和箭头符号,如下所示:

如果我们说

 *ipp = ip2;

我们改变了ipp(即ip1)指向的指针,以包含ip2的副本,这样它(ip1) 现在指向 j:

<小时>

我的问题是:为什么在第二张图中,ipp 仍然指向 ip1 而不是 ip2?

解决方案

暂时忘记指向类比.指针真正包含的是内存地址.& 是运算符的地址" - 即它返回对象在内存中的地址.* 运算符为您提供一个指针所指的对象,即给定一个包含地址的指针,它返回该内存地址处的对象.因此,当您执行 *ipp = ip2 时,您所做的是 *ipp 获取位于 ipp 中保存的地址的对象,即 ip1,然后将ip2中存储的值赋值给ip1,即j的地址.

简单
& -->
的地址* --> 值在

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 to ip1 which points to i. *ipp is ip1, and **ipp is i, 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 of ip2, so that it (ip1) now points at j:


My question is: Why in the second picture, is ipp still pointing to ip1 but not ip2?

解决方案

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 in ipp which is ip1 and then assign to ip1 the value stored in ip2, which is the address of j.

Simply
& --> Address of
* --> Value at

这篇关于指向指针的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆