指针的指针澄清 [英] Pointer to pointer clarification

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

问题描述

我是按照此教程了解如何做一个的指针的指针的工作。

让我引述相关段落:



  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 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天全站免登陆