【问题标题】:How is this variable of type pointer to pointer这个指针类型的变量如何指向指针
【发布时间】:2023-03-23 20:22:01
【问题描述】:
struct node
{
   int data;
   struct node *link;
};

struct node *addnode(struct node **head);

int main()
{
    struct node *head = NULL;

    addnode(&head);

    return 0;
}


struct node *addnode(struct node **ptrTohead)
{
       if (*ptrTohead == NULL)
       {
          struct node *newNode = (struct node*) malloc(sizeof(struct node));
          *ptrTohead = newNode;
       }

}

我正在用 C 语言实现链表并遇到了这段代码:我不明白 &head 的类型是 struct node ** 毕竟 *head 是一个存储地址的指针,而 @987654325 @ 获取头变量的地址。那么它是一个指向指针的指针呢?

这就是我的想象:

//                head ----> |___2______| 
/memory address/  100           200
// &head is 100 and is of type struct node *

【问题讨论】:

  • head 是一个struct node* 并获取head 的地址会得到指向struct node* 的指针,这是一个struct node** (必须有一个重复?)
  • 您刚刚描述了如何拥有一个指针,并且您将指针指向它。那怎么不是指向指针的指针?
  • "*head 是一个指针" ...不,head 是一个指针。 *head 取消引用该指针(并且是 struct node 类型)。指针的地址是指向指针的指针。
  • 所以基本上取head的地址,给出一个指针的地址(head)和head指向某物,因此将这两个想法结合在一起意味着它是一个结构节点** ?我是否正确地考虑了这一点,这太令人困惑了哈哈
  • 是的。对于任何类型T,指向T 的指针是T*。指向T* 的指针是T**

标签: c struct dereference pointer-to-pointer


【解决方案1】:

研究这个简单的演示程序。

#include <stdio.h>

int main(void) 
{
    int x = 10;
    
    int *p = &x;

    printf( "The variable x stores the value %d\n", x );
    printf( "Its address is %p\n", ( void * )&x );
    
    printf( "\nThe pointer p stores the address of the variable x %p\n", 
            ( void * )p );
    printf( "Dereferencing the pointer p we will get x equal to %d\n" , *p );
    printf( "The address of the pointer p itself is %p\n", ( void * )&p );
    
    int **pp = &p;
    
    printf( "\nThe pointer pp stores the address of the pointer p %p\n", 
            ( void * )pp );
    printf( "Dereferencing the pointer pp we will get the address stored in p %p\n",
            ( void * )*pp );
    printf( "Dereferencing the pointer pp two times "
            "we will get  x equal to %d\n", **pp );

    int y = 20;
    
    printf( "\nBecause dereferencing the pointer pp\n"
            "we have a direct access to the value stored in p\n"
            "we can change the value of p\n" );

    *pp = &y;           

    printf( "\nNow the pointer p stores the address of the variable y %p\n", 
            ( void * )p );
    printf( "Dereferencing the pointer p we will get y equal to %d\n" , *p );
    printf( "The address of the pointer p itself was not changed %p\n", ( void * )&p );
    
    return 0;
}

程序输出可能看起来像

The variable x stores the value 10
Its address is 0x7fffd5f6de90

The pointer p stores the address of the variable x 0x7fffd5f6de90
Dereferencing the pointer p we will get x equal to 10
The address of the pointer p itself is 0x7fffd5f6de98

The pointer pp stores the address of the pointer p 0x7fffd5f6de98
Dereferencing the pointer pp we will get the address stored in p 0x7fffd5f6de90
Dereferencing the pointer pp two times we will get  x equal to 10

Because dereferencing the pointer pp
we have a direct access to the value stored in p
we can change the value of p

Now the pointer p stores the address of the variable y 0x7fffd5f6de94
Dereferencing the pointer p we will get y equal to 20
The address of the pointer p itself was not changed 0x7fffd5f6de98

也就是说变量ppp都是指针。区别在于指针p 指向int 类型的对象(指向变量xy),而指针pp 指向类型为p 的变量@987654331 @。取消引用这些指针中的任何一个,您都可以直接访问指向的对象并可以更改它(如果它不是常量对象)。

【讨论】:

  • 谢谢,这很有帮助,但我仍然不明白为什么 &head 是指向指针的指针,您可以编辑您的答案并请回答
  • @programmerc3981143 因为 head 本身就是一个指针结构 node *head = NULL;现在再调查一下我的答案中显示的程序。
【解决方案2】:

main 中,head 是一个指向结构节点的指针。表达式&amp;head 是指向结构节点的指针的地址。 ptrTohead 是函数addnode 的形参,是指向结构节点的指针。 &amp;head 的值可以分配给ptrTohead,因为它是指向结构节点的指针的地址,而指向结构节点的指针的地址是用于初始化指向结构节点的指针的适当值。

注意&amp;head 不是指向结构节点指针的指针。它是指向结构节点的指针的地址。另请注意,*head 不是指针。 *head 是一个结构节点,而head 是一个指向结构节点的指针。

【讨论】:

  • 如果&amp;head不是指向指针的指针,为什么它的类型为struct node **,是不是因为表达式&amp;head的每一级都有一个单独的值,并将它们放在一起得到struct node**level 表示此表达式中地址的深度。所以&amp;head 给出了指针的地址,它是struct node * 类型的1 级,然后取消引用它会给出另一个struct node* 类型的地址。这两种类型加在一起得到struct node**。嗯,这就是我的想法,至少这@WilliamPursell 有多正确
  • 这可能是语义上的狡辩。表达式&amp;head 计算为指向结构节点的指针的地址。可以为struct node ** 类型的变量分配一个值,该值是指向结构节点的指针的地址。
  • 啊,我明白了,但我的想法是否足够,或者这是不正确的?
  • 我不知道您所说的“地址深度”是什么意思。我发现总是更容易想到T *h,这意味着h是指向T的指针。所以&amp;h是指向T的指针的地址。Tint还是struct foo ***无关紧要。
猜你喜欢
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-11
  • 2014-05-18
  • 1970-01-01
相关资源
最近更新 更多